首页 技术 正文
技术 2022年11月15日
0 收藏 754 点赞 3,059 浏览 3521 个字

根据url 生成指定尺寸的二维码图片

UIImage * createBinaryCodeImg(const char * url ,CGFloat size)
{
//create binary code image with the url
CIFilter *ciFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[ciFilter setDefaults];
NSString *showUrl = [NSString stringWithCString:url encoding:NSUTF8StringEncoding];
NSData *nsData = [showUrl dataUsingEncoding:NSUTF8StringEncoding];
[ciFilter setValue:nsData forKey:@"inputMessage"];
CIImage *image = [ciFilter outputImage]; //fixing the image to the size
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, , , cs, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage); CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}

把生成的二维码贴到底图上,里面嵌套调用了生成二维码图片的方法

UIImage* createBinaryCodeImageByUrl(const char * url,UIImage *bottomImg,int drawAtPositionX,int drawAtPositionY,int binaryCodeImgWidth)
{
UIImage *binaryImg = createBinaryCodeImg(url, binaryCodeImgWidth); CGImageRef imageRef = bottomImg.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef); size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef); CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef);
CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef); CFDataRef data = CGDataProviderCopyData(dataProvider);
UInt8* buffer = (UInt8*)CFDataGetBytePtr(data); CGImageRef binaryImgRef = binaryImg.CGImage;
CGDataProviderRef binaryImgDataPvd = CGImageGetDataProvider(binaryImgRef);
CFDataRef datacpy = CGDataProviderCopyData(binaryImgDataPvd);
UInt8 *binaryImgBuffer = (UInt8 *)CFDataGetBytePtr(datacpy);
size_t binaryBitsPerRow = CGImageGetBytesPerRow(binaryImgRef); NSUInteger x, y,w,h;
int limitWidth = drawAtPositionX + binaryCodeImgWidth;
int limitHeight = drawAtPositionY + binaryCodeImgWidth;
for (y = drawAtPositionY,h = ; y < limitHeight; y++,h++) {
for (x = drawAtPositionX,w = ; x < limitWidth; x++,w++) {
UInt8* tmp,*binaryTmp;
tmp = buffer + y * bytesPerRow + x * ;
binaryTmp = binaryImgBuffer + h * binaryBitsPerRow + w * ; *(tmp + ) = *(binaryTmp + );
*(tmp + ) = *(binaryTmp + );
*(tmp + ) = *(binaryTmp + );
}
} CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data)); CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData); CGImageRef effectedCgImage;
UIImage* effectedImage;
effectedCgImage = CGImageCreate(
width, height,
bitsPerComponent, bitsPerPixel, bytesPerRow,
colorSpace, bitmapInfo, effectedDataProvider,
NULL, shouldInterpolate, intent);
effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage]; CGImageRelease(effectedCgImage);
CFRelease(effectedDataProvider);
CFRelease(effectedData);
CFRelease(data); CFRelease(datacpy); return effectedImage;
}

贴二维码到底图上应该有更好的方法,在网上有看到上下文直接绘图的方式,对ios开发不熟悉,东拼西凑把功能做出来了。

参考:

http://www.cnblogs.com/smileK/p/9554552.html

http://outofmemory.cn/code-snippet/14937/UIImage-picture-process

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844