我们如何从当地获取图像?

时间:2011-01-22 06:41:03

标签: iphone ios

我通过此代码将图像存储在本地...

NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageName = [NSString stringWithFormat:@"image.png"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

[imageData writeToFile:fullPathToFile atomically:YES];

我可以在我的iphone模拟器套装中看到这个图像...但是我该如何取回这张图片呢?

1 个答案:

答案 0 :(得分:2)

如果您的意思是从文件中重新创建图像,则可以使用UIImage的{​​{3}}方法非常简单地执行此操作:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:@"someImageName"];
UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];