1.NSString的分类
生成纯的二维码:
#import "NSString+Extension.h"
#import <CoreImage/CoreImage.h>
@implementation NSString (Extension)
-(UIImage *)createRRcodeWithSize:(CGFloat)size
{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
CIImage *outputImage = [filter outputImage];
CGRect extent = CGRectIntegral(outputImage.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/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:outputImage 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];
}
@end
UIImage的分类,对生成的二维码进行处理(添加中间icon)
#import "UIImage+Operatioin.h"
@implementation UIImage (Operatioin)
+(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize {
UIGraphicsBeginImageContext(image.size);
CGFloat widthOfImage = image.size.width;
CGFloat heightOfImage = image.size.height;
CGFloat widthOfIcon = iconSize.width;
CGFloat heightOfIcon = iconSize.height;
[image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
[icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
widthOfIcon, heightOfIcon)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end