Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save UIView's representation to file

What is the easiest way to save UIView's representation to file?

My solution is,

 UIGraphicsBeginImageContext(someView.frame.size);
 [someView drawRect:someView.frame];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 NSString* pathToCreate = @"sample.png";

 NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
 [imageData writeToFile:pathToCreate atomically:YES];

but it seems tricky, and I think there must be more efficient way to do this.

like image 330
fish potato Avatar asked Jun 16 '10 08:06

fish potato


1 Answers

You can also use the layer like this:

UIGraphicsBeginImageContext(someView.bounds.size);
[someView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
like image 165
Michael Kessler Avatar answered Sep 23 '22 22:09

Michael Kessler