获取UIImage像素数据

来源:互联网 发布:恐怖黎明物品数据库 编辑:程序博客网 时间:2024/06/06 00:35

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



0 0