2011年6月9日星期四

The code to change UIView to images

NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

- (UIImage *)imageFromView:(UIView *)myView{
if (UIGraphicsBeginImageContextWithOptions!=NULL)
{
UIGraphicsBeginImageContextWithOptions(myView.bounds.size,NO,0.0);
}
else
{
UIGraphicsBeginImageContext(myView.bounds.size);
}
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [[viewImage retain] autorelease];
}