zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    第一篇博客
    Word2vec负采样
    Ubuntu系统为应用建立桌面快捷方式(以Pycharm为例)
    Kaggle入门Titanic——模型建立
    Kaggle入门Titanic——特征工程
    ubuntu系统theano和keras的安装
    win7系统下python安装numpy,matplotlib,scipy和scikit-learn
    ubuntu14.04环境下spyder的安装
    防止IE7,8进入怪异模式
    自定义列表
  • 原文地址:https://www.cnblogs.com/abelmou/p/9934827.html
Copyright © 2011-2022 走看看