Saving images to a given location

岁酱吖の 提交于 2019-12-02 09:01:14
Thomas Huber

You can use NSData to store the image data.

To save the image:

 NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];  
[imageData writeToFile:imagePath atomically:YES];

To retrieve the image:

 UIImage *myImage = [UIImage imageWithContentsOfFile:imagePath];

Update #1: Example (I can't test the example right now but it should work this way):

 UIGraphicsBeginImageContext(self.view.bounds.size);  
self.view.layer renderInContext:UIGraphicsGetCurrentContext()];  
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();   
// Get the location of the Documents directory  
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;  
NSString *imagePath = [paths objectAtIndex:0];  
NSString *filename = @"test.png" ;   
NSString *filepath = [NSString stringWithFormat:@"%@/%@", imagePath, filename];  
UIGraphicsEndImageContext();  
// Save the image   
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(myImage)];  
[imageData writeToFile:filepath atomically:YES];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!