美文网首页
CGBitmapContext介绍

CGBitmapContext介绍

作者: lltree | 来源:发表于2018-03-01 16:35 被阅读117次

    使用CGBitmapContextCreate创建绘制图片的上下文

    
    CGContextRef CGBitmapContextCreate (
      void *data,
      size_t width,
      size_t height,
      size_t bitsPerComponent,
      size_t bytesPerRow,
      CGColorSpaceRef colorspace,
      CGBitmapInfo bitmapInfo
      );
    
    • 参数data指向绘图操作被渲染的内存区域,这个内存区域大小应该为(bytesPerRow*height)个字节。如果对绘制操作被渲染的内存区域并无特别的要求,那么可以传递NULL给参数date。
    • 参数width代表被渲染内存区域的宽度。
    • 参数height代表被渲染内存区域的高度。
    • 参数bitsPerComponent被渲染内存区域中组件在屏幕每个像素点上需要使用的bits位,举例来说,如果使用32-bit像素和RGB颜色格式,那么RGBA颜色格式中每个组件在屏幕每个像素点上需要使用的bits位就为32/4=8。
    • 参数bytesPerRow代表被渲染内存区域中每行所使用的bytes位数。
    • 参数colorspace用于被渲染内存区域的“位图上下文”。
    • 参数bitmapInfo指定被渲染内存区域的“视图”是否包含一个alpha(透视)通道以及每个像素相应的位置,除此之外还可以指定组件式是浮点值还是整数值。

    而传入参数呢信息又可以从位图上下文中获取

    
    CG_EXTERN void * __nullable CGBitmapContextGetData(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the width of the bitmap context `context', or 0 if `context' is
       not a bitmap context. */
    
    CG_EXTERN size_t CGBitmapContextGetWidth(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the height of the bitmap context `context', or 0 if `context' is
       not a bitmap context. */
    
    CG_EXTERN size_t CGBitmapContextGetHeight(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the bits per component of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    CG_EXTERN size_t CGBitmapContextGetBitsPerComponent(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the bits per pixel of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    CG_EXTERN size_t CGBitmapContextGetBitsPerPixel(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the bytes per row of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    CG_EXTERN size_t CGBitmapContextGetBytesPerRow(CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the color space of the bitmap context `context', or NULL if
       `context' is not a bitmap context. */
    
    CG_EXTERN CGColorSpaceRef __nullable CGBitmapContextGetColorSpace(
        CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the alpha info of the bitmap context `context', or
       "kCGImageAlphaNone" if `context' is not a bitmap context. */
    
    CG_EXTERN CGImageAlphaInfo CGBitmapContextGetAlphaInfo(
        CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Return the bitmap info of the bitmap context `context', or 0 if `context'
       is not a bitmap context. */
    
    CG_EXTERN CGBitmapInfo CGBitmapContextGetBitmapInfo(
        CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
    

    最终根据位图上下文创建一个CGImageRef

    CG_EXTERN CGImageRef __nullable CGBitmapContextCreateImage(
        CGContextRef cg_nullable context)
        CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);
    
    + (UIImage *)mosaicImage:(UIImage *)image withLevel:(int)level
    {
        unsigned char *imgPixel = RequestImagePixelData(image);
        CGImageRef inImageRef = [image CGImage];
        GLuint width = CGImageGetWidth(inImageRef);
        GLuint height = CGImageGetHeight(inImageRef);
        unsigned char prev[4] = {0};
        int bytewidth = width*4;
        int i,j;
        int val = level;
        for(i=0;i<height;i++) {
            if (((i+1)%val) == 0) {
                memcpy(imgPixel+bytewidth*i, imgPixel+bytewidth*(i-1), bytewidth);
                continue;
            }
            for(j=0;j<width;j++) {
                if (((j+1)%val) == 1) {
                    memcpy(prev, imgPixel+bytewidth*i+j*4, 4);
                    continue;
                }
                memcpy(imgPixel+bytewidth*i+j*4, prev, 4);
            }
        }
        NSInteger dataLength = width*height* 4;
        
        //下面的代码创建要输出的图像的相关参数
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        //创建要输出的图像
        CGImageRef imageRef = CGImageCreate(width, height,
                                            bitsPerComponent,
                                            bitsPerPixel,
                                            bytewidth,
                                            colorSpaceRef,
                                            bitmapInfo,
                                            provider,
                                            NULL, NO, renderingIntent);
        UIImage *mosaicImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
        CFRelease(imageRef);
        CGColorSpaceRelease(colorSpaceRef);
        CGDataProviderRelease(provider);
        return mosaicImage;
    }
    

    相关文章

      网友评论

          本文标题:CGBitmapContext介绍

          本文链接:https://www.haomeiwen.com/subject/afxnxftx.html