iOS中生成指定大小、指定颜色的二维码和条形码方法详解中生成指定大小、指定颜色的二维码和条形码方法详解
本文主要介绍了iOS中生成指定大小、指定颜色的二维码和条形码的具体实现方法,具有一定的参考价值,下面跟着小编一起来看下吧
iOS7.0之后可以利用系统原生 API 生成二维码, iOS8.0之后可以生成条形码, 系统默认生成的颜色是黑色. 在这里, 利用以下方法可以生成指定大小、指定
颜色的二维码和条形码, 还可以添加背景颜色、阴影效果, 以下是具体方法.
一一. 生成二维码生成二维码
Avilable in iOS 7.0 and later
方法如下方法如下:
#pragma mark - 生成二维码
//Avilable in iOS 7.0 and later
+ (UIImage *)qrCodeImageWithContent:(NSString *)content
codeImageSize:(CGFloat)size
logo:(UIImage *)logo
logoFrame:(CGRect)logoFrame
red:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue;
具体实现如下具体实现如下:
#pragma mark - 生成二维码
+ (UIImage *)qrCodeImageWithContent:(NSString *)content
codeImageSize:(CGFloat)size
logo:(UIImage *)logo
logoFrame:(CGRect)logoFrame
red:(CGFloat)red
green:(CGFloat)green
blue:(CGFloat)blue{
UIImage *image = [self qrCodeImageWithContent:content codeImageSize:size red:red green:green blue:blue];
//有 logo 则绘制 logo
if (logo != nil) {
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[logo drawInRect:logoFrame];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}else{
return image;
}
}
//改变二维码颜色
+ (UIImage *)qrCodeImageWithContent:(NSString *)content codeImageSize:(CGFloat)size red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{
UIImage *image = [self qrCodeImageWithContent:content codeImageSize:size];
int imageWidth = image.size.width;
int imageHeight = image.size.height;
size_t bytesPerRow = imageWidth * 4;
uint32_t *rgbImageBuf = (uint32_t *)malloc(bytesPerRow * imageHeight);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpaceRef, 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*255;
ptr[2] = green*255;
ptr[1] = blue*255;
}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, colorSpaceRef,
kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
UIImage *resultImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpaceRef);
return resultImage;
}
//改变二维码尺寸大小
+ (UIImage *)qrCodeImageWithContent:(NSString *)content codeImageSize:(CGFloat)size{
CIImage *image = [self qrCodeImageWithContent:content];
CGRect integralRect = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(integralRect), size/CGRectGetHeight(integralRect));
size_t width = CGRectGetWidth(integralRect)*scale;
size_t height = CGRectGetHeight(integralRect)*scale;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:integralRect];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, integralRect, bitmapImage);
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);