• iOS开发CGImage.h简介


    1、前因

      由于剪切图片用到下面方法,此方法属于CGImage.h中,通过创建CGImageRef像素位图,可以通过操作存储的像素位来编辑图片。

    /* Create an image using the data contained within the subrectangle `rect'
     of `image'.
     截取图像的一个区域重绘图像*/
    CG_EXTERN CGImageRef __nullable CGImageCreateWithImageInRect(
                                                                 CGImageRef cg_nullable image, CGRect rect)

      效果图展示:

      代码展示:

        UIImage *bgImage =[UIImage imageNamed:@"1111"];
    
        // 上半部
        UIImageView *topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 140, CScreenWidth, (CScreenHeight-140) * 0.5)];
        topImgView.image = [self clipImage:bgImage withRect:CGRectMake(0, 0, imageW, imageH)];
        
        // 下半部
        UIImageView *bottomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 140+(CScreenHeight-140) * 0.5, CScreenWidth, (CScreenHeight-140) * 0.5)];
        bottomImgView.image = [self clipImage:bgImage withRect:CGRectMake(0, imageH, imageW, imageH)];
        [self.view addSubview:topImgView];
        [self.view addSubview:bottomImgView];
    
        CGAffineTransform transform = CGAffineTransformIdentity;
        transform =CGAffineTransformScale(transform, .5, .5);
        [UIView animateWithDuration:2.0f animations:^{
            CGRect topRect = topImgView.frame;
            topRect.origin.x -= CScreenWidth;
            topImgView.frame = topRect;
            topImgView.transform = transform;
            
            CGRect bottomRect = bottomImgView.frame;
            bottomRect.origin.x += CScreenWidth;
            bottomImgView.frame = bottomRect;
            bottomImgView.transform = transform;
        } completion:^(BOOL finished) {
            [topImgView removeFromSuperview];
            [bottomImgView removeFromSuperview];
        }];

    - (UIImage *)clipImage:(UIImage *)image withRect:(CGRect)rect {
        CGRect clipFrame = rect;
        CGImageRef refImage = CGImageCreateWithImageInRect(image.CGImage, clipFrame);
        UIImage *newImage = [UIImage imageWithCGImage:refImage];
        CGImageRelease(refImage);
        return newImage;
    }

    2、CGImage.h文件方法简介

    typedef CF_ENUM(uint32_t, CGImageAlphaInfo) {
        kCGImageAlphaNone,               /* For example, RGB. */
        kCGImageAlphaPremultipliedLast,  /* For example, premultiplied RGBA */
        kCGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */
        kCGImageAlphaLast,               /* For example, non-premultiplied RGBA */
        kCGImageAlphaFirst,              /* For example, non-premultiplied ARGB */
        kCGImageAlphaNoneSkipLast,       /* For example, RBGX. */
        kCGImageAlphaNoneSkipFirst,      /* For example, XRGB. */
        kCGImageAlphaOnly                /* No color data, alpha data only */
    };
    
    typedef CF_ENUM(uint32_t, CGImageByteOrderInfo) {
        kCGImageByteOrderMask     = 0x7000,
        kCGImageByteOrderDefault  = (0 << 12),
        kCGImageByteOrder16Little = (1 << 12),
        kCGImageByteOrder32Little = (2 << 12),
        kCGImageByteOrder16Big    = (3 << 12),
        kCGImageByteOrder32Big    = (4 << 12)
    } CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
    
    typedef CF_OPTIONS(uint32_t, CGBitmapInfo) {
        kCGBitmapAlphaInfoMask = 0x1F,
        
        kCGBitmapFloatInfoMask = 0xF00,
        kCGBitmapFloatComponents = (1 << 8),
        
        kCGBitmapByteOrderMask     = kCGImageByteOrderMask,
        kCGBitmapByteOrderDefault  = kCGImageByteOrderDefault,
        kCGBitmapByteOrder16Little = kCGImageByteOrder16Little,
        kCGBitmapByteOrder32Little = kCGImageByteOrder32Little,
        kCGBitmapByteOrder16Big    = kCGImageByteOrder16Big,
        kCGBitmapByteOrder32Big    = kCGImageByteOrder32Big
    } CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
    
    #ifdef __BIG_ENDIAN__
    # define kCGBitmapByteOrder16Host kCGBitmapByteOrder16Big
    # define kCGBitmapByteOrder32Host kCGBitmapByteOrder32Big
    #else    /* Little endian. */
    # define kCGBitmapByteOrder16Host kCGBitmapByteOrder16Little
    # define kCGBitmapByteOrder32Host kCGBitmapByteOrder32Little
    #endif
    
    /* Return the CFTypeID for CGImageRefs. */
    CG_EXTERN CFTypeID CGImageGetTypeID(void)
    
    /* Create an image.
     width:图片宽度像素
     height:图片高度像素
     bitsPerComponent:每个颜色的比特数,例如在rgba-32模式下为8
     bitsPerPixel:每个像素的总比特数
     bytesPerRow:每一行占用的字节数,注意这里的单位是字节
     space:颜色空间模式,例如const CFStringRef kCGColorSpaceGenericRGB 这个函数可以返回一个颜色空间对象。
     bitmapInfo:位图像素布局
     provider:数据源提供者
     decode[]:解码渲染数组
     shouldInterpolate:是否抗锯齿
     intent:图片相关参数*/
    CG_EXTERN CGImageRef __nullable CGImageCreate(size_t width, size_t height,
                                                  size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,
                                                  CGColorSpaceRef cg_nullable space, CGBitmapInfo bitmapInfo,
                                                  CGDataProviderRef cg_nullable provider,
                                                  const CGFloat * __nullable decode, bool shouldInterpolate,
                                                  CGColorRenderingIntent intent)
    
    /* Create an image mask.
     创建mask图片图层,可以设置其显示部分与不显示部分达到特殊的效果*/
    CG_EXTERN CGImageRef __nullable CGImageMaskCreate(size_t width, size_t height,
                                                      size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,
                                                      CGDataProviderRef cg_nullable provider, const CGFloat * __nullable decode,
                                                      bool shouldInterpolate)
    
    /* Return a copy of `image'. Only the image structure itself is copied; the
     underlying data is not.
     复制一个CGImageRef对象*/
    CG_EXTERN CGImageRef __nullable CGImageCreateCopy(CGImageRef cg_nullable image)
    
    /* Create an image from `source', a data provider of JPEG-encoded data.
    通过JPEG数据源获取图像 */
    CG_EXTERN CGImageRef __nullable CGImageCreateWithJPEGDataProvider(
                                                                      CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode,
                                                                      bool shouldInterpolate,
                                                                      CGColorRenderingIntent intent)
    
    /* Create an image using `source', a data provider for PNG-encoded data.
    通过PNG数据源获取图像 */
    CG_EXTERN CGImageRef __nullable CGImageCreateWithPNGDataProvider(
                                                                     CGDataProviderRef cg_nullable source, const CGFloat * __nullable decode,
                                                                     bool shouldInterpolate,
                                                                     CGColorRenderingIntent intent)
    
    /* Create an image using the data contained within the subrectangle `rect'
     of `image'.
     截取图像的一个区域重绘图像*/
    CG_EXTERN CGImageRef __nullable CGImageCreateWithImageInRect(
                                                                 CGImageRef cg_nullable image, CGRect rect)
    
    /* Create a new image from `image' masked by `mask', which may be an image
     mask or an image.
    截取mask图像的某个区域重绘图像 */
    CG_EXTERN CGImageRef __nullable CGImageCreateWithMask(
                                                          CGImageRef cg_nullable image, CGImageRef cg_nullable mask)
    
    /* Create a new image from `image' masked by `components'
     通过颜色分量数组创建位图*/
    CG_EXTERN CGImageRef __nullable CGImageCreateWithMaskingColors(
                                                                   CGImageRef cg_nullable image, const CGFloat * cg_nullable components)
    
    /* Create a copy of `image', replacing the image's color space with `space'.
     Returns NULL if `image' is an image mask, or if the number of components
     of `space' isn't the same as the number of components of the color space
     of `image'.
     通过颜色空间模式复制位图*/
    CG_EXTERN CGImageRef __nullable CGImageCreateCopyWithColorSpace(
                                                                    CGImageRef cg_nullable image, CGColorSpaceRef cg_nullable space)
    
    /* Equivalent to `CFRetain(image)'.
     引用计数+1*/
    CG_EXTERN CGImageRef cg_nullable CGImageRetain(CGImageRef cg_nullable image)
    
    /* Equivalent to `CFRelease(image)'.
     引用计数-1*/
    CG_EXTERN void CGImageRelease(CGImageRef cg_nullable image)
    
    /* Return true if `image' is an image mask, false otherwise.
    是否是mask图层 */
    CG_EXTERN bool CGImageIsMask(CGImageRef cg_nullable image)
    
    /* Return the width of `image'.
     获取图像宽度像素*/
    CG_EXTERN size_t CGImageGetWidth(CGImageRef cg_nullable image)
    
    /* Return the height of `image'.
     获取图像的高度像素*/
    CG_EXTERN size_t CGImageGetHeight(CGImageRef cg_nullable image)
    
    //获取图像相应属性
    /* Return the number of bits/component of `image'. */
    CG_EXTERN size_t CGImageGetBitsPerComponent(CGImageRef cg_nullable image)
    /* Return the number of bits/pixel of `image'. */
    CG_EXTERN size_t CGImageGetBitsPerPixel(CGImageRef cg_nullable image)
    /* Return the number of bytes/row of `image'. */
    CG_EXTERN size_t CGImageGetBytesPerRow(CGImageRef cg_nullable image)
    /* Return the color space of `image', or NULL if `image' is an image
     mask. */
    CG_EXTERN CGColorSpaceRef __nullable CGImageGetColorSpace(CGImageRef cg_nullable image)
    /* Return the alpha info of `image'. */
    CG_EXTERN CGImageAlphaInfo CGImageGetAlphaInfo(CGImageRef cg_nullable image)
    /* Return the data provider of `image'. */
    CG_EXTERN CGDataProviderRef __nullable CGImageGetDataProvider(CGImageRef cg_nullable image)
    /* Return the decode array of `image'. */
    CG_EXTERN const CGFloat * __nullable CGImageGetDecode(CGImageRef cg_nullable image)
    /* Return the interpolation parameter of `image'. */
    CG_EXTERN bool CGImageGetShouldInterpolate(CGImageRef cg_nullable image)
    /* Return the rendering intent of `image'. */
    CG_EXTERN CGColorRenderingIntent CGImageGetRenderingIntent(cg_nullable CGImageRef image)
    /* Return the bitmap info of `image'. */
    CG_EXTERN CGBitmapInfo CGImageGetBitmapInfo(CGImageRef cg_nullable image)
    /* Return the UTType of `image'. */
    CG_EXTERN CFStringRef  __nullable CGImageGetUTType(cg_nullable CGImageRef image)
  • 相关阅读:
    zabbix_server调优
    zabbix图形刷新延迟解决
    ntp同步报错解决
    python3.5 字典遍历
    python3.5 append使用
    python3.5 元组
    pycharm修改代码后第一次运行不生效解决
    Intellij IDEA 使用Spring-boot-devTools无效解决办法
    TOP命令各个参数代表意义详解
    ps命令各个内容信息详解
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8694350.html
Copyright © 2020-2023  润新知