如何调用本地图像文件并显示到imageviewcontroller

我创build了一个应用程序,在应用程序不被删除的同时创build本地存储支付文件。

这我已经做了:我下载文件并存储到本地(应用程序文档文件夹)。

现在我遇到了这个问题:当图像被下载后,我将进入本地,该图像不会显示。 在NSLog下面的URL生成,我不能显示图像。

/Users/username/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150×150.jpg

编辑:

NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", @"/Users/username/Library/Application Support/iPhone Simulator/5.0/Applications/F233ED88-1546-4FB0-BE93-0E0FB8C8C3D6/Documents/NationalMedalofArts-150x150.jpg"]]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; NSLog(@"%@",imageData); UIImage *image = [UIImage imageWithData:imageData]; self.imgView.image = image; 

当NSLog的NSData然后它是NULL显示

这个更好:

 NSString *fileName = @"NationalMedalofArts-150x150.jpg"; NSString *path = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName]; self.imgView.image = [UIImage imageWithContentsOfFile:path]; 

尝试使用这个:

 NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString* file = [documentsPath stringByAppendingPathComponent:@"NationalMedalofArts-150x150.jpg"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:file]; UIImage *image = [UIImage imageWithData:imageData]; self.imgView.image = image; 

当访问应用程序的文档文件夹中的文件时,必须使用第一行来查找文档文件夹的path。 上面的代码应该适合你。