保存UIImageView图像

我目前有能力从相机拍摄照片或加载,并在我的应用程序UIImageView中放置图像是我想有一个button,将保存图像的能力,所以当我重新加载应用程序的图像仍然在的UIImageView。

我虽然是一个button来保存图像,一旦你已经加载它和在

-(void)viewDidLoad 

有代码来加载保存的图像,但我不知道如何处理这个。

任何帮助将是太棒了。

您可以使用UIImagePNGRepresentationUIImageJPGRepresentation将图像转换为NSData ,然后将数据保存到调用NSDatawriteToFile...方法之一的文件中。

然后,当你重新启动你的应用程序,你可以通过调用[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]来获取图像

– 编辑 –

保存图片

 UIImage *image = ...; NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"]; [UIImagePNGRepresentation(image) writeToFile:cachedImagePath atomically:YES]; 

加载图片

 NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image.png"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:cachedImagePath]]; 

我使用NSCachesDirectory因为我假设你不想有这个图像备份(通过iCloud或iTunes)。 如果你这样做,你会想要使用NSDocumentDirectory