根据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. C++学习笔记(7)----类的数组中构造函数和析构函数的调用顺序

    C++类的数组中构造函数和析构函数的调用顺序(2) 对于如下的代码: #include<iostream> using namespace std; class CBase { priva ...

  2. SSH反向代理转发至内网msf

    前言 买了个便宜的 vps , 在上面装 msf 也装不上,于是想着把端口映射到内网来. 正文 拓扑如下: 首先在内网主机 B ssh -fCNR 7281:localhost:5000 root@C ...

  3. Android解析WindowManagerService(一)WMS的诞生

    前言 此前我用多篇文章介绍了WindowManager,这个系列我们来介绍WindowManager的管理者WMS,首先我们先来学习WMS是如何产生的.本文源码基于Android 8.0,与Andro ...

  4. [转载]一步一步教你如何在Virtualbox虚拟机中安装Remix

    原文地址:https://bbs.jide.com/forum.php?mod=viewthread&tid=4892 大神请路过-- [准备工具] 1.Virtualbox虚拟机(这个是免费 ...

  5. Servlet:从入门到实战学习(3)---Servlet实例【图文】

    本篇通过图文实例给大家详细讲述如何建立一个Servlet,配置好运行环境并成功连接到MYSQL的数据库,进行数据的查询展示. 1.项目创建:IDEA -> Create New Project ...

  6. file cycle

    # Author:Alex# Date:2017.06.07# Version:3.6.0with open('james.txt') as jaf: data = jaf.readline() ja ...

  7. 关于Angular中时间戳的计算

    前言 使用的是Moment.js 插件,插件的安装详情请参考官方网址(https://momentjs.com/) 正文 步骤一:引用import * as moment from 'moment'; ...

  8. 五、python小功能记录——打包程序

    使用pyinstaller打包Python程序 安装工具 :pip3 install pyinstaller 在Python程序文件夹上(不点进去)按住shift并且右键,在弹出的选项中点击" ...

  9. [翻译] CNPGridMenu

    CNPGridMenu CNPGridMenu is a Mailbox style grid menu with a blurred background for iOS 7 & iOS 8 ...

  10. Python初学者第十七天 函数(1)

    17day 函数 1.函数定义: 函数 是指将一组语句的集合通过一个名字(函数名)封装起来,想要执行这个函数,只需调用其函数名即可 2.函数的特性: a 减少重复代码 b 使程序变得可扩展 c 使程序 ...