#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"1"];
// 截取屏幕位置
CGImageRef dst = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 50, 50));
// 将截取的屏幕放在另一个view中
_imageView.image = [UIImage imageWithCGImage:dst];
}
- (IBAction)paly:(id)sender {
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_imageView.image = image;
NSData *jpgData = UIImageJPEGRepresentation(image, 0);
[jpgData writeToFile:@"/Users/apple/Desktop/test.jpg" atomically:YES];
NSData *pngData = UIImagePNGRepresentation(image);
[pngData writeToFile:@"/Users/apple/Desktop/test.png" atomically:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end