简体   繁体   English

xcode5:如何保存和加载图像?

[英]xcode5: how can I save and load an image?

I'm developing an app, where I can apply filters on an image.我正在开发一个应用程序,我可以在其中对图像应用过滤器。
I want to create an undo button which resets the original image in the imageView .我想创建一个撤销按钮,在重置原始图像imageView
The solution is that I just save the original image in it's own UIImage object before I apply any filters to it.解决方案是,在对它应用任何过滤器之前,我只是将原始图像保存在它自己的UIImage对象中。 This way I can just go back to that in my undo method.这样我就可以在我的撤消方法中回到那个状态。

Does somebody know how I can do this?有人知道我该怎么做吗?

Take a look at the docs here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html看看这里的文档: https : //developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html

You can do what you want with UIImagePNGRepresentation() .你可以用UIImagePNGRepresentation()做你想做的事。

Then to re-read: imageWithContentsOfFile:然后重新阅读: imageWithContentsOfFile:

Get Image from disk . Get Image from disk Suppose image stored in document directory .假设image storeddocument directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *yourImgPath = [documentsDirectory stringByAppendingPathComponent:@"1.png"];

UIImage *originalImage = [UIImage imageWithContentsOfFile:yourImgPath];

Create tempImage from originalImageoriginalImage创建tempImage

UIImage *tempImage = originalImage;

Appy filters on tempImage

EDIT : How to save then below is method to save in document directory .编辑:如何save ,然后下面是method在保存document directory

- (BOOL)saveImageInDocDir:(UIImage *)image withImagename:(NSString *)strImgName {
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strImgName];
  NSData *imageData = UIImagePNGRepresentation(image);
  BOOL isSaved = [imageData writeToFile:savedImagePath atomically:YES];   
  return isSaved;
}

You can also use NSTempraryDirectory to save images as its safe from cloud backup您还可以使用NSTempraryDirectory save imagescloud backup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM