how to save the image in system which is captured from UIGraphicsGetImageFromCurrentImageContext() in iPhone?

StackOverflow https://stackoverflow.com/questions/22603604

Question

Hi I want to capture the current UIView as image and store in my local hard disc how can i achieve:

Example :

UIGraphicsBeginImageContext(self.superview.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

i want to store UIImage in my local drive in any of the png, jpeg format.

How can i do this one. Kindly help me out. Thanks in advance.

Était-ce utile?

La solution 2

The following code may help you to achieve.

UIGraphicsBeginImageContext(self.superview.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[data writeToFile:fileName atomically:YES];

Autres conseils

To store image in Document folder of App.

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

NSString *dir = [searchPaths lastObject];

NSString *imagePath = [dir stringByAppendingPathComponent:@"/image.png"];

NSData *dataImage=UIImageJPEGRepresentation(image, 0);

[dataImage writeToFile:imagePath atomically:YES];

The new way (method) is available from Apple:

- (UIImage *)snapshot:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];
    [UIImageJPEGRepresentation(image, 0.95) writeToFile:imagePath atomically:YES];

    return image;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top