//生成二维码
- (UIImage *)generateQRCodeWithString:(NSString *)string Size:(CGFloat)size
{
//创建过滤器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//过滤器恢复默认
[filter setDefaults];
//给过滤器添加数据<字符串长度893>
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[filter setValue:data forKey:@"inputMessage"];
//获取二维码过滤器生成二维码
CIImage *image = [filter outputImage];
UIImage *img = [self createNonInterpolatedUIImageFromCIImage:image WithSize:size];
return img;
}
//二维码清晰
- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image WithSize:(CGFloat)size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
//创建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);
//保存图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
- (void)loadImageFinished:(UIImage *)image
{
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
[MBProgressHUD showSuccess:@"图片已保存到相册"];
NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}
-(void)loadImage{
// [self loadImageFinished:[self generateQRCodeWithString:[Config getUserInviteUrl] Size:100]];
[self combineImge];
}
-(void)copyLink{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [Config getUserInviteUrl];
[MBProgressHUD showSuccess:@"已复制到粘贴板"];
}
- (UIImage *)snapshotImageWithView:(UIView *)view {
UIGraphicsBeginImageContext(view.bounds.size); //currentView 当前的view 创建一个基于绘图的图形上下文并指定大小为当前视图的bounds
[view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
return viewImage;
}
- (UIImage *)blurImageWithImage:(UIImage *)image {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:3.0] forKey:@"inputRadius"];
// CIImage *result = [filter valueForKey:kCIOutputImageKey];
CIImage *result=[filter outputImage];
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
UIImage *img = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return img;
}
- (void)combineImge {
UIImage *imgOrigin = [UIImage imageNamed:@"图层 4"];
UIImage *img = [self blurImageWithImage:imgOrigin];
CGFloat w = [UIScreen mainScreen].bounds.size.width;
CGFloat h = [UIScreen mainScreen].bounds.size.height;
//以1.png的图大小为底图
UIImage *img1 = [self snapshotImageWithView:self.qrBGView];
CGImageRef imgRef1 = img1.CGImage;
CGFloat w1 = CGImageGetWidth(imgRef1);
CGFloat h1 = CGImageGetHeight(imgRef1);
//以img 图大小为画布创建上下文
UIGraphicsBeginImageContext(CGSizeMake(w, h));
[img drawInRect:CGRectMake(0, 0, w, h)];
[img1 drawInRect:CGRectMake((w - w1)/2.0, (h - h1)/2.0, w1, h1)];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
UIGraphicsEndImageContext();//关闭上下文
//存储图片
// UIImageWriteToSavedPhotosAlbum(resultImg, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
网友评论