一、生成分享所需二维码
所需头文件
#import <CoreImage/CoreImage.h>
生成分享的二维码
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSString *string = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@",@"AppId"];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[filter setValue:data forKeyPath:@"inputMessage"];
CIImage *image = [filter outputImage];
以上image就得到了所需二维码。不过这个二维码有点模糊,下面做高清处理。
二、图片高清处理
- (UIImage *)hdImageWithUIImage:(CIImage *)image width:(CGFloat)imageWidth {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(imageWidth/CGRectGetWidth(extent), imageWidth/CGRectGetHeight(extent));
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];
}
三、二维码添加logo
以上得到了二维码,不过太单调了。我们在二维码的中间放上App的logo。
得到App的logo代码
NSString *icon = [[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
shareImageView.image = [UIImage imageNamed:icon];
以下是整套代码
CGFloat imageWidth = AUTO_W(225);
UIImageView *shareImageView = [[UIImageView alloc] init];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSString *string = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@",[VKLConfigParse parserConfig].AppId];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[filter setValue:data forKeyPath:@"inputMessage"];
CIImage *image = [filter outputImage];
UIImage *qrImage = [self hdImageWithUIImage:image width:imageWidth];
NSString *icon = [[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
UIImage *iconImage = [self hdImageWithUIImage:[UIImage imageNamed:icon].CIImage width:imageWidth * 0.3];
UIGraphicsBeginImageContext(CGSizeMake(imageWidth, imageWidth));
[qrImage drawInRect:CGRectMake(0, 0, imageWidth, imageWidth)];
[iconImage drawInRect:CGRectMake((imageWidth - iconImage.size.width) * 0.5, (imageWidth - iconImage.size.height) * 0.5, iconImage.size.width, iconImage.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
shareImageView.image = resultingImage;
网友评论