你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 生成二維碼

生成二維碼

編輯:IOS開發綜合

生成二維碼


- (void)viewDidLoad {

[super viewDidLoad];

self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 300, 300)];

// imgView.backgroundColor = [UIColor redColor];

[self.view addSubview: self.imgView];

//創立過濾器

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

//設置默許

[filter setDefaults];

//過濾器添加數據

NSString * dataString = @"wlwlwlwlwlwlwll";

NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

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

//獲取輸入的二維碼

CIImage * img = [filter outputImage];

//設置img 的像素

UIImage * img1 =[self createNonInterpolatedUIImageFormCIImage:img withSize:1000];

//設置img的顏色

self.imgView.image = [self imageBlackToTransparent:img1 withRed:255 andGreen:0 andBlue:0];

}


//由於生成的二維碼是一個CIImage,我們直接轉換成UIImage的話大小不好控制,所以運用上面辦法前往需求大小的UIImage:

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {

CGRect extent = CGRectIntegral(image.extent);

CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

// 創立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);

// 保管bitmap到圖片

CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

CGContextRelease(bitmapRef);

CGImageRelease(bitmapImage);

return [UIImage imageWithCGImage:scaledImage];

}


//3.設置圖片通明度

void ProviderReleaseData (void *info, const void *data, size_t size){

free((void*)data);

}

- (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{

const int imageWidth = image.size.width;

const int imageHeight = image.size.height;

size_t bytesPerRow = imageWidth * 4;

uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,

kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);

CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

// 遍歷像素

int pixelNum = imageWidth * imageHeight;

uint32_t* pCurPtr = rgbImageBuf;

for (int i = 0; i < pixelNum; i++, pCurPtr++){

if ((*pCurPtr & 0xFFFFFF00) < 0x99999900) // 將白色變成通明

{

// 改成上面的代碼,會將圖片轉成想要的顏色

uint8_t* ptr = (uint8_t*)pCurPtr;

ptr[3] = red; //0~255

ptr[2] = green;

ptr[1] = blue;

}

else

{

uint8_t* ptr = (uint8_t*)pCurPtr;

ptr[0] = 0;

}

}

// 輸入圖片

CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);

CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,

kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,

NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];

// 清算空間

CGImageRelease(imageRef);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

return resultUIImage;

}


【生成二維碼】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved