获取UIImage像素数据

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 */
};

//Create a bitmap context

CG_EXTERN CGContextRef CGBitmapContextCreate(

void *data, //如果data非空,指向一个内存区域,大小至少bytesPerRow *height的字节。如果data为空,data上下文自动分配和释放收回。

size_t width, //像素宽

size_t height, //像素高

size_t bitsPerComponent,  //位的数量为每个组件的指定一个像素

size_t bytesPerRow, //每一行的位图的字节,至少要宽*每个像素的字节

CGColorSpaceRef space,  //指定一个颜色空间

CGBitmapInfo bitmapInfo //指定位图是否应该包含一个alpha通道和它是如何产生的,以及是否组件是浮点或整数

)


//获取UIImage像素ARGB值

- (unsigned char *)getImageData:(UIImage*)image

{

CGImageRef imageref = [image CGImage];
    CGColorSpaceRef colorspace=CGColorSpaceCreateDeviceRGB();
       
    int width=CGImageGetWidth(imageref);
    int height=CGImageGetHeight(imageref);
    int bytesPerPixel=4;
    int bytesPerRow=bytesPerPixel*width;
    int bitsPerComponent = 8;
    
    unsigned char * imagedata=malloc(width*height*bytesPerPixel);
   
    CGContextRef cgcnt = CGBitmapContextCreate(imagedata,
                                               width,
                                               height,
                                               bitsPerComponent,
                                               bytesPerRow,
                                               colorspace,
                                               kCGImageAlphaPremultipliedFirst);
    //将图像写入一个矩形
    CGRect therect = CGRectMake(0, 0, width, height);
    CGContextDrawImage(cgcnt, therect, imageref);
    

//释放资源
    CGColorSpaceRelease(colorspace);

CGContextRelease(cgcnt);    
    
    for (int i = 0 ; i < 4*width*height; i++) {
        NSLog(@"UIImage(%d,%d) == %d",(i/4)%width,(i/4)/width,imagedata[i]);
        if (i%4==3) {
          NSLog(@"=================");
          }
}
                
    return imagedata;
}


//获取UIImage像素Gray值

- (unsigned char *)getImageData:(UIImage*)image

{

CGImageRef imageref = [image CGImage];

CGColorSpaceRef colorspace=CGColorSpaceCreateDeviceGray();
       
     int width=CGImageGetWidth(imageref);
     int height=CGImageGetHeight(imageref);
     int bytesPerPixel=1;
     int bytesPerRow=bytesPerPixel*width;
     int bitsPerComponent = 8;
    
      unsigned char * imagedata=malloc(width*height*bytesPerPixel);
   
   CGContextRef cgcnt = CGBitmapContextCreate(imagedata,
                                               width,
                                               height,
                                               bitsPerComponent,
                                               bytesPerRow,
                                               colorspace,
                                               kCGImageAlphaNone);
     //将图像写入一个矩形
     CGRect therect = CGRectMake(0, 0, width, height);
     CGContextDrawImage(cgcnt, therect, imageref);
    

//释放资源
     CGColorSpaceRelease(colorspace);

CGContextRelease(cgcnt);    
    
     for (int i = 0 ; i < width*height; i++) {
         NSLog(@"UIImage(%d,%d) == %d", i%width, i/width, imagedata[i]);
}
                
    return imagedata;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值