生成二維碼

  • 我們簡單的使用一下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;

}

畫一個頭像上去,看一下效果:
二維碼 效果

發佈了177 篇原創文章 · 獲贊 111 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章