首页 > 安全资讯 >

生成二维码

16-01-20

我们简单的使用一下CIFilter来完成二维码的生成,解剖一下二维码的生成过程 查找到 kCICategoryBuiltIn 内建的滤镜名 NSArray *filterNames = [CIFilter filterNamesInCategory:kCICategoryBuilt

我们简单的使用一下CIFilter来完成二维码的生成,解剖一下二维码的生成过程
查找到 kCICategoryBuiltIn 内建的滤镜名
NSArray *filterNames = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];

    NSLog(@"%@",filterNames);

二维码

找到这个CIQRCodeGenerator
@property (nonatomic, strong) CIFilter *filter;
 _filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

   NSArray *inputKeys =  _filter.inputKeys;

keys

inputMessage 表示 二维码的内容, 参数类型是NSData
 NSData *data =  [content dataUsingEncoding:NSUTF8StringEncoding];
    [_filter setValue:data forKey:@"inputMessage"];

   CIImage *ciImage =  _filter.outputImage;
  ciImage =   [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    UIImage *image = [UIImage imageWithCIImage:ciImage];

  image =   [image imageWithIcon:[UIImage imageNamed:@"wohao"]];
    self.imageView.image = image;
给UIImage 写一个分类
- (UIImage *)imageWithIcon:(UIImage *)icon
{
    //开启上下文
    UIGraphicsBeginImageContext(self.size);
    //画原图
    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];

    //在中间画用户头像,宽高为20%,正方形
    CGFloat WH = MIN(self.size.width, self.size.height) * 0.2;
    [icon drawInRect:CGRectMake((self.size.width - WH) * 0.5, (self.size.height - WH) * 0.5, WH, WH)];

   UIImage *newImage =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}

画一个头像上去,看一下效果:
二维码 效果

 
 
相关文章
最新文章
热点推荐