Цей фрагмент коду показує, як перетворити представлення на UIImage
:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
І цей фрагмент показує, як зберегти UIImage
як JPEG або PNG:
NSString *pngPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES];
[UIImagePNGRepresentation(viewImage) writeToFile:pngPath atomically:YES];