iOS 截屏 保存到相册

2016-07-20  本文已影响300人  Cz1024

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);    //设置截屏大小

//    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);//这里可以设置想要截图的区域

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:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

[alert show];

}else{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败,你的设置里的隐私设置,可能拒绝了,XXXXX访问你的照片" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

[alert show];

}

}

上一篇 下一篇

猜你喜欢

热点阅读