无法显示我的图像(.png)。
我检查它是否存在于我使用的路径中,它确实存在。但是没有图像出现。
这是我一直在尝试的:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *outputPath = [documentsDir stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
NSString *imgPath = [[outputPath stringByReplacingOccurrencesOfString:@"mov" withString:@"png"]retain];
if ([fileManager fileExistsAtPath:imgPath])
{
NSLog(@"FOUND IMG");
NSLog(imgPath);
}
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
[imgView setImage:thumbNail];
[cell addSubview:imgView];
[imgView release];确认文件存在的调试输出
FOUND IMG
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B-
9509-EF68B1A0E720/Documents/0.009000.png我有点困惑。有没有人看到我在哪里犯了错?
非常感谢,-Code
我尝试了其他一些方法,但什么都没有出现。
发布于 2011-12-26 20:50:59
您应该使用[NSURL fileURLWithPath:imgPath]而不是[NSURL URLWithString:imgPath]。图像路径不是有效的url,它需要file://前缀。
发布于 2011-12-26 21:06:02
试着使用
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];或
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];发布于 2011-12-26 20:55:55
你需要给出正确的文件url,这将是
NSURL *imgURL = [NSURL fileURLWithPath:imgPath];然后,使用适当的imgURL变量初始化图像,最后,要将图像添加到单元,您需要执行以下操作
[cell.contentView addSubview:imgView];我希望它能解决你的问题。
https://stackoverflow.com/questions/8635909
复制相似问题