//截屏代码
UIGraphicsBeginImageContext(CGSizeMake(1024, MAIN_SCREEN_HITHT));//设置截屏尺寸
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(CGSizeMake(1024, MAIN_SCREEN_HITHT));//设置截屏尺寸
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
image就是截取后的图片
另一种
- //截图
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);
- [self.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- CGImageRef imageRef = viewImage.CGImage;
- CGRect rect =CGRectMake(100, 100, 200, 300);//这里可以设置想要截图的区域
- CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
- UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
- NSData *imageViewData = UIImagePNGRepresentation(sendImage);
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"result.png"];
- NSLog(@"%@", savedImagePath);
- [imageViewData writeToFile:savedImagePath atomically:YES];
- CGImageRelease(imageRefRect);