Saving images to a given location

2019-03-05 05:54发布

I want to take screenshots of the iPhone app view and save the images to a given location.

My code below saves the images to the photo library, but I want to save it to some other given location. Is it possible to do so? Please help me up.

My code is here:

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

1条回答
祖国的老花朵
2楼-- · 2019-03-05 06:28

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];
查看更多
登录 后发表回答