iOS 根据url生成二维码贴到底图上

时间:2022-05-07 22:24:38

根据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