在iOS 5中显示已保存的图像

时间:2011-11-22 17:05:26

标签: ios uiimage nsdata

我有一个应用程序,我将图像保存到NSDocumentDirectory。我可以看到路径设置正确,但是当我尝试检索它们时,NSData为空。

以下是我的一些片段..

NSData *imageData  = UIImagePNGRepresentation(image);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"myApp"];

//Create unique filename
CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
path = [path stringByAppendingPathComponent:(__bridge_transfer NSString *)newUniqueIdString];
path = [path stringByAppendingPathExtension: @"png"];

[imageData writeToFile:path atomically:NO];         

[self showPhoto:path];

然后在我的showPhoto方法

NSData *pngData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:pngData];

如果我调试pngData它是0字节,但我的路径显示我认为正确的路径?

如果照片实际上在手机上,是否有任何调试方法?我不确定它是否得到了保存。

2 个答案:

答案 0 :(得分:1)

你可以这样做并检查文件是否存在并使用imageWithContentsOfFile:来检索它。

NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:localFilePath];
bool fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];

if(fileExists == YES)
{
    UIImage *_image = [UIImage imageWithContentsOfFile:localFilePath]
}

如果图像仍为空,则表示图像有问题。

答案 1 :(得分:1)

如果您在模拟器中运行应用程序,可以在此处找到应用程序文件:/ Users / {user} / Library / Application Support / iPhone Simulator / 5.0 / Applications

如果您在iPhone上进行调试,可以使用iExplorer查看iPhone上的文件。