//切图
- (UIImage*)screenView:(UIView *)view{
CGRect rect = view.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
//切图并保存到相册中
-(void)savaImageToPhotosAlbumWithImage:(UIImage *)viewImage{
CGImageRef imageRef = viewImage.CGImage;
CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域
//CGRect rect = CGRectMake(0, 0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库
// NSData *imageViewData = UIImagePNGRepresentation(sendImage);
CGImageRelease(imageRefRect);
}
//将图片保存到沙盒目录下
-(void)savaImageToDomainMaskWithIamge:(UIImage *)image{
NSData *imageData = UIImagePNGRepresentation(image);
/*
*设置图片存储位置
*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pictureName = @"picture.png";
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];
[imageData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录
}