1.截取指定的view
-(UIImage *)captureImageFromViewLow:(UIView *)orgView {
//獲取指定View的圖片
UIGraphicsBeginImageContextWithOptions(orgView.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[orgView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2.截取View上任意位置 信柿,順帶保存到相冊
- (void)saveQRCodeImgToPhotoAlbum{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(LCDW, LCDH), NO, 0.0);? ? //設(shè)置截屏大小
//? ? UIGraphicsBeginImageContext(CGSizeMake(LCDW,LCDH));
[[self layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = viewImage.CGImage;
CGRect rect = CGRectMake(2*self.bgroundView.frame.origin.x,2*self.bgroundView.frame.origin.y-15, 2*self.bgroundView.frame.size.width,2*self.bgroundView.frame.size.height);//這里可以設(shè)置想要截圖的區(qū)域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
UIImageWriteToSavedPhotosAlbum(sendImage, self, @selector ( image:didFinishSavingWithError:contextInfo:), nil);//保存圖片到照片庫
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手機(jī)相冊" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失敗,你的設(shè)置里的隱私設(shè)置,可能拒絕了,XXXXX訪問你的照片" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
}
}