QRimg.image=[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:self.model.link] withSize:150];
- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
// 1.實(shí)例化二維碼濾鏡
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢復(fù)濾鏡的默認(rèn)屬性 (因?yàn)闉V鏡有可能保存上一次的屬性)
[filter setDefaults];
// 3.將字符串轉(zhuǎn)換成NSdata
NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
// 4.通過KVO設(shè)置濾鏡, 傳入data, 將來濾鏡就知道要通過傳入的數(shù)據(jù)生成二維碼
[filter setValue:data forKey:@"inputMessage"];
// 5.生成二維碼
CIImage *outputImage = [filter outputImage];
return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.創(chuàng)建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.復(fù)制文本
UIPasteboard * pastboard = [UIPasteboard generalPasteboard];
pastboard.string = _model.code;