1.生成二维码图片
-(void)createQrcodeImage{
UIView * qrcodeView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/2-70, getframeOY(self.topimageView)-25, 140, 140)];
qrcodeView.backgroundColor = WhiteBackColor;
qrcodeView.layer.masksToBounds = YES;
qrcodeView.layer.borderColor = [RGBA(229, 229, 229, 1.0) CGColor];
qrcodeView.layer.borderWidth = 0.8f;
qrcodeView.userInteractionEnabled = YES;
[self.scrollView addSubview:qrcodeView];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 130, 130)];
[qrcodeView addSubview:self.imageView];
// 1.创建过滤器,这里的@"CIQRCodeGenerator"是固定的
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复默认设置
[filter setDefaults];
// 3. 给过滤器添加数据
NSString * accouStr = [LYCommanManager sharedManager].account;
if (StringIsEmpty(accouStr)) {
accouStr = @"";
}
NSString * accountStr = [NSString stringWithFormat:@"%@",accouStr];
NSString * urlStr = [NSString stringWithFormat:@"mobile/user/invite_reg/invitecode/%@",accountStr];
NSString * shareUrl = [TESTBASEURL_Share stringByAppendingString:urlStr];
NSString *dataString = shareUrl;
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
// 注意,这里的value必须是NSData类型
[filter setValue:data forKeyPath:@"inputMessage"];
// 4. 生成二维码
CIImage *outputImage = [filter outputImage];
// 5. 显示二维码
self.imageView.image = [self creatNonInterpolatedUIImageFormCIImage:outputImage withSize:220];
UILabel * baocunLabel = [UILabel wh_labelWithText:@"微信扫一扫即可邀请好友注册" textFont:15 textColor:RGBA(51, 51, 51, 1.0) frame:CGRectMake(MARGIN_OX, getframeOY(qrcodeView)+15, ScreenWidth-MARGIN_OX*2, 20)];
[self.scrollView addSubview:baocunLabel];
UIButton * shareBtn = [UIButton wh_buttonWithTitle:@"保存到相册" backColor:RGBA(255, 84, 84, 1.0) backImageName:nil titleColor:WhiteTextColor fontSize:15 frame:CGRectMake(ScreenWidth/2-115, getframeOY(baocunLabel)+25, 230, 44) cornerRadius:22];
[self.scrollView addSubview:shareBtn];
[shareBtn wh_addActionHandler:^{
if ( [SPMobileApplication sharedInstance].isLogined == NO){
[[LYCommonMBprogresshud sharedManager]showProgresshudTextWith:self.view title:@"您还未登录,请先登录..." detailtitle:nil isOnDismissbg:NO hideAfterdelay:YES];
return;
}
[self saveImageView];
}];
self.scrollView.contentSize = CGSizeMake(ScreenWidth, getframeOY(shareBtn)+30);
}
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*
* @return 生成高清的UIImage
*/
- (UIImage *)creatNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1. 创建bitmap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
2.将要展示的图片保存到相册
//截屏分享
-(void)saveImageView{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];
// 这个是要分享图片的样式(自定义的)
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//保存到本地相机
UIImageWriteToSavedPhotosAlbum(newImage,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}
//保存相片的回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
// [[AXProgressHUDHelper getInstance]showTextWithStatus:@"保存失败" onView:self.view];
UIAlertController *controller=[UIAlertController alertControllerWithTitle:@"去设置页面->APP名称->照片、相机" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sure=[UIAlertAction actionWithTitle:@"去开启权限" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
dispatch_async(dispatch_get_main_queue(), ^{
if( [[UIApplication sharedApplication] canOpenURL:url]){
[[UIApplication sharedApplication] openURL:url];
}
});
}];
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"暂不" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[controller addAction:sure];
[controller addAction:cancel];
[self presentViewController:controller animated:YES completion:^{
}];
} else {
[[AXProgressHUDHelper getInstance]showSuccessWithStatus:@"成功保存到相册" onView:self.view];
}
}
这样就实现了保存图片到相册功能了。