码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-11-09 16:02:23      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:RoCE   tar   tip   gets   draw   绘图   qrcode   memory   pix   

根据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生成二维码贴到底图上

标签:RoCE   tar   tip   gets   draw   绘图   qrcode   memory   pix   

原文地址:https://www.cnblogs.com/abelmou/p/9934827.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!