从文档目录加载图像

19 iphone xcode objective-c ios

我无法显示我的图像(.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];
Run Code Online (Sandbox Code Playgroud)

确认文件的调试输出存在

FOUND IMG
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B- 
9509-EF68B1A0E720/Documents/0.009000.png
Run Code Online (Sandbox Code Playgroud)

我有点困惑.谁知道我犯了哪些错误?

非常感谢,-Code

我尝试了其他几种方法,但没有出现任何方法.

YuA*_*uAo 21

尝试使用

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
Run Code Online (Sandbox Code Playgroud)

要么

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
Run Code Online (Sandbox Code Playgroud)


iHu*_*ter 17

你应该使用[NSURL fileURLWithPath:imgPath]而不是[NSURL URLWithString:imgPath].图像路径不是有效的URL,它需要一个file://前缀.