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); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } //生成最原始的二維碼 + (CIImage *)qrCodeImageWithContent:(NSString *)content{ CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding]; [qrFilter setValue:contentData forKey:@"inputMessage"]; [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"]; CIImage *image = qrFilter.outputImage; return image; } void ProviderReleaseData (void *info, const void *data, size_t size){ free((void*)data); }
一. 生成條形碼
Avilable in iOS 8.0 and later
辦法如下:
#pragma mark - 生成條形碼 //Avilable in iOS 8.0 and later + (UIImage *)barcodeImageWithContent:(NSString *)content codeImageSize:(CGSize)size red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
詳細完成如下:
#pragma mark - 生成條形碼 + (UIImage *)barcodeImageWithContent:(NSString *)content codeImageSize:(CGSize)size red:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue{ UIImage *image = [self barcodeImageWithContent: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 *)barcodeImageWithContent:(NSString *)content codeImageSize:(CGSize)size{ CIImage *image = [self barcodeImageWithContent:content]; CGRect integralRect = CGRectIntegral(image.extent); CGFloat scale = MIN(size.width/CGRectGetWidth(integralRect), size.height/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); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } //生成最原始的條形碼 + (CIImage *)barcodeImageWithContent:(NSString *)content{ CIFilter *qrFilter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"]; NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding]; [qrFilter setValue:contentData forKey:@"inputMessage"]; [qrFilter setValue:@(0.00) forKey:@"inputQuietSpace"]; CIImage *image = qrFilter.outputImage; return image; } void ProviderReleaseData (void *info, const void *data, size_t size){ free((void*)data); }
三.測試
次要代碼如下:
//-----------------------------------二維碼、條形碼測試-------------------------------------------------- //條形碼 UIImage *barImage = [Utilities barcodeImageWithContent:@"123456" codeImageSize:CGSizeMake(300, 90) red:0 green:0.4 blue:0.6]; CGRect barImageView_Frame = CGRectMake(self.view.bounds.size.width/2-300/2, 100, 300, 90); UIImageView *barImageView = [[UIImageView alloc] initWithFrame:barImageView_Frame]; barImageView.image = barImage; barImageView.backgroundColor = [UIColor clearColor]; //暗影 barImageView.layer.shadowOffset = CGSizeMake(-0.5, 0.5); barImageView.layer.shadowRadius = 0.5; barImageView.layer.shadowColor = [UIColor blackColor].CGColor; barImageView.layer.shadowOpacity = 0.2; [self.view addSubview:barImageView]; //二維碼 UIImage *qrCodeImage = [Utilities qrCodeImageWithContent:@"How are you?" codeImageSize:200 logo:[UIImage imageNamed:@"logo.png" />運轉後果
以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持本站!
【iOS中生成指定大小、指定顏色的二維碼和條形碼辦法詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!