[英]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 stored
在document 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 originalImage
从
originalImage
创建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 images
在cloud backup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.