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

iOS 根据url生成二维码贴到底图上的更多相关文章

  1. Android 把url生成二维码并贴到给定的底图上

    主要是用到了com.google.zxing jar包生成二维码的功能,这个jar包需要自己接下载 直接上代码 public static Bitmap CreateBinaryCodeImageBy ...

  2. iOS开发之生成二维码

    一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器   1.二维码的内容(传统的条形码只能放数字) 纯文本 名片 URL   2.生成二 ...

  3. SpringMVC将url生成二维码图片直接展示在页面上

    利用google的开源包zxing生成二维码 第一步:maven项目的zxing依赖 <!-- google zxing 生成二维码 --> <dependency> < ...

  4. JAVA根据URL生成二维码图片、根据路径生成二维码图片

    引入jar包 zxing-2.3.0.jar.IKAnalyzer2012_u6.jar 下载地址:https://yvioo.lanzous.com/b00nlbp6h                ...

  5. iOS 原生实现生成二维码(CoreImage)工具类,不依赖第三方库,可自定义背景颜色,添加logo(Swift 4.0)

    import Foundation import CoreImage import UIKit extension UIColor { var coreImageColor: CIColor { re ...

  6. 几个常用的url生成二维码的接口

    找到了几个URL生成的接口,速度上可能会有差别,可试验后选用,我用过第一个,分享: <!doctype html> <html lang="en"> < ...

  7. java url生成二维码保存到本地

    http://blog.sina.com.cn/s/blog_5a6efa330102v1lb.html http://blog.csdn.net/about58238/article/details ...

  8. IOS开发技巧快速生成二维码

    随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...

  9. java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字

    前段时间因为工作需要,要实现将一段文字或者url生成二维码,然后中间附上logo,下方正中间附上文字的功能. 上网找了几篇教程学习了下,由于没有保存借鉴的博文链接,所以就没po上参考文章的链接啦,感谢 ...

随机推荐

  1. Dinic算法----最大流常用算法之一

    ——没有什么是一个BFS或一个DFS解决不了的:如果有,那就两个一起. 最大流的$EK$算法虽然简单,但时间复杂度是$O(nm^2)$,在竞赛中不太常用. 竞赛中常用的$Dinic$算法和$SAP$, ...

  2. JavaWeb学习总结(六):HttpServletRespone对象(二)

    一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: package gacl.r ...

  3. React 入门实例教程[阮一峰的网络日志] (分享)

    作者: 阮一峰 https://github.com/ruanyf/react-demos 转自:http://www.ruanyifeng.com/blog/2015/03/react.html 对 ...

  4. java indexOf 和 split的用法

    1.java 的 indexOf 方法 ,如果存在 则 指定的字符串的开始位置,如果不存在 则返回-1: 2.java 的 split的方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. ...

  5. 为什么使用.Net Core, Asp.net Core以及部署到云端

    一,基于微软技术平台开发的应用项目,如果有以下的需求和潜在需求,建议使用.Net Core和Asp.net core技术架构: 1. 跨平台支持 2. 采用微服务架构 3. 使用Docker容器 4. ...

  6. linux系统PKWindows系统,从各方便分析linux和Windows的优劣

    服务器系统linux系统和linux系统哪个好用,公说公有理婆说婆有理,今天鼎峰凡凡大概对Linux系统与Windows系统的优缺点PK!可以从以下几个方面来看 ①成本  赞成Linux的声音Linu ...

  7. VS 2012 在 windows 8 中无法使用 Deubgger.Lunch() 对服务进行调试

    找到了外文资料:   Debugger.Launch() not displaying JIT debugger selection popup on Windows 8/8.1   If execu ...

  8. cetnos7下openresty使用luarocks 进行lua的包管理

    先安装一下包管理工具 yum install luarocks lua-devel -y luarocks install lpack ln -s /usr/lib64/lua /usr/local/ ...

  9. linux 下 eclipse 开发环境的搭建

    在windows平台下开发习惯了各种ide,不得不承认ide给我们开发带来的便捷.于是希望在linux平台下也希望找到一个合适的ide.这就想起来了eclipse具有跨平台性. 那么就开始搭建环境吧. ...

  10. scala当中的特质trait

    1.将trait作为接口使用 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java中的接口 (interface)非常类似: 在trait中可以定义 ...