iOS 二維碼的生成

#import <CoreImage/CoreImage.h>

/** 生成二維碼*/

- (void)set2DimensionalBarCode:(UIImageView *)imgView

{

// 1.實(shí)例化二維碼濾鏡

CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

// 2.恢復(fù)濾鏡的默認(rèn)屬性

[filter setDefaults];

// 3.二維碼信息

//NSString *str = @"哎呀我去"; // 展示一串文字

NSString *str = @"http://www.baidu.com"; // 直接打開網(wǎng)頁(yè)

// 4.將字符串轉(zhuǎn)成二進(jìn)制數(shù)據(jù)

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

// 5.通過(guò)KVC設(shè)置濾鏡inputMessage數(shù)據(jù)

[filter setValue:data forKey:@"inputMessage"];

// 6.獲取濾鏡輸出的圖像

CIImage *outputImage = [filter outputImage];

// 7.將CIImage轉(zhuǎn)成UIImage

//系統(tǒng)默認(rèn)獲取到的二維碼圖片不太清晰,可以利用這個(gè)方法,獲取指定大小(傳入的size參數(shù)值)的二維碼圖片

UIImage *image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

// 8.展示二維碼

imgView.image = image;

}

/**

*? 根據(jù)CIImage生成指定大小的UIImage

*

*? @param image CIImage

*? @param size? 圖片寬度

*/

- (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];

}

轉(zhuǎn)自 YotrolZ ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 分享的內(nèi)容,大家看了后,不管有什么問(wèn)題或者建議,都可以說(shuō)出來(lái),我都會(huì)一一做答,一起加油啦 我的后面兩章文章< >,...
    邵瓊閱讀 809評(píng)論 4 0
  • 一、生成二維碼 1.首先要導(dǎo)入CoreImage框架 #import <Corelmage/CoreImage.h...
    archyly閱讀 424評(píng)論 0 0
  • ios 7 之后系統(tǒng)集成了二維碼 所以ios 7 之前的zbarsdk 不支持64為處理器1 導(dǎo)入CoreImag...
    cj小牛閱讀 721評(píng)論 0 3
  • 一、掃描 1、 ZBar ZBar在掃描的靈敏度上,和內(nèi)存的使用上相對(duì)于ZXing上都是較優(yōu)的,但是對(duì)于 “圓角二...
    空白Null閱讀 1,143評(píng)論 0 2
  • 自定義二維碼,就是指給系統(tǒng)二維碼添加一些圖片(前景或者背景圖片), 或者改變下顏色 自定義二維碼實(shí)質(zhì)是通過(guò)Quar...
    翻這個(gè)墻閱讀 2,397評(píng)論 0 0

友情鏈接更多精彩內(nèi)容