iOS 根据url生成二维码贴到底图上
Posted _我头大
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 根据url生成二维码贴到底图上相关的知识,希望对你有一定的参考价值。
根据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, 8, 0, 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 = 0; y < limitHeight; y++,h++) { for (x = drawAtPositionX,w = 0; x < limitWidth; x++,w++) { UInt8* tmp,*binaryTmp; tmp = buffer + y * bytesPerRow + x * 4; binaryTmp = binaryImgBuffer + h * binaryBitsPerRow + w * 4; *(tmp + 0) = *(binaryTmp + 0); *(tmp + 1) = *(binaryTmp + 1); *(tmp + 2) = *(binaryTmp + 2); } } 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
以上是关于iOS 根据url生成二维码贴到底图上的主要内容,如果未能解决你的问题,请参考以下文章
关于android中使用SQLite数据库的查询基本操作.贴上代码.