iOS UIImage與Bytes Pixel之間轉換

  • iOS視頻圖像開發中無法避免使用到UIImage與Bytes Pixel之間轉換。bytes pixel可以理解為將一張圖片的所有像素點寫到二維數組中。
  • 本篇文章中將圖片UIImage與BGRA pixel之間轉換為例講解。
iOS UIImage與Pixel轉換
  • 首先簡單看下圖片生成上下文的CGBitmapContextCreate方法的原型及參數。
/**
*  @param data                 指向要渲染的繪制內存的地址。這個內存塊的大小至少是(bytesPerRow*height)個字節
*  @param width                bitmap的寬度,單位為像素
*  @param height               bitmap的高度,單位為像素
*  @param bitsPerComponent     內存中像素的每個組件的位數.例如,對于32位像素格式和RGB顏色空間,你應該將這個值設為8.
*  @param bytesPerRow          bitmap的每一行在內存所占的比特數
*  @param space                bitmap上下文使用的顏色空間
*  @param bitmapInfo           指定bitmap是否包含alpha通道,像素中alpha通道的相對位置,像素組件是整形還是浮點型等信息的字符串。
*/

CGContextRef CGBitmapContextCreate(void *data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef colorspace, CGBitmapInfo bitmapInfo);
  • UIImage對象轉換為BGRA
- (unsigned char *)pixelBRGABytesFromImage:(UIImage *)image {
    return [self pixelBRGABytesFromImageRef:image.CGImage];
}

- (unsigned char *)pixelBRGABytesFromImageRef:(CGImageRef)imageRef {
    
    NSUInteger iWidth = CGImageGetWidth(imageRef);
    NSUInteger iHeight = CGImageGetHeight(imageRef);
    NSUInteger iBytesPerPixel = 4;
    NSUInteger iBytesPerRow = iBytesPerPixel * iWidth;
    NSUInteger iBitsPerComponent = 8;
    unsigned char *imageBytes = malloc(iWidth * iHeight * iBytesPerPixel);
    
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate(imageBytes,
                                                 iWidth,
                                                 iHeight,
                                                 iBitsPerComponent,
                                                 iBytesPerRow,
                                                 colorspace,
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    
    CGRect rect = CGRectMake(0 , 0 , iWidth , iHeight);
    CGContextDrawImage(context , rect ,imageRef);
    CGColorSpaceRelease(colorspace);
    CGContextRelease(context);
    CGImageRelease(imageRef);
    
    return imageBytes;
}
  • 使用方法
UIImage *image = [UIImage imageNamed:@"pict1.png"];
unsigned char *imageBytes = [self pixelBRGABytesFromImage:image];
//注:不要忘記釋放malloc的內存
free(imageBytes);
  • BGRA轉換為UIImage對象
- (UIImage *)imageFromBRGABytes:(unsigned char *)imageBytes imageSize:(CGSize)imageSize {
    CGImageRef imageRef = [self imageRefFromBGRABytes:imageBytes imageSize:imageSize];
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return image;
}

- (CGImageRef)imageRefFromBGRABytes:(unsigned char *)imageBytes imageSize:(CGSize)imageSize {
 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(imageBytes,
                                                imageSize.width,
                                                imageSize.height,
                                                8,
                                                imageSize.width * 4,
                                                colorSpace,
                                                kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    return imageRef;
}
  • 使用方法
UIImage *image = [self imageFromBRGABytes:imageBytes imageSize:imageSize];
NSLog(@"image = %@",image);
  • 注意
    CGImageRef為Core Graphics框架中的結構體指針。原型為:
typedef struct CGImage *CGImageRef;

即使在ARC中也需要手動釋放對象,調用CGImageRelease或者CFRelease釋放。

  • 擴展
  • UIImage轉換為Gray pixel灰色圖方法:
    將獲取CGColorSpaceRef方法修改CGColorSpaceCreateDeviceGray(),并iBytesPerPixel的4改為1即可。原先用4byte BGRA來描述圖片,現在只用1byte描述灰度。
  • 實例中Alpha選項為kCGImageAlphaPremultipliedFirst,該選項中的premultiplied代表著圖片的BGR值是已乘以Alpha值,因此想得到該像素點的BGR值。
CGFloat oldAlpha = 255.0 / imageBytes[3];
CGFloat newAlpha = (CGFloat)newAlphaValue / 255;
imageBytes[0] *= oldAlpha * newAlpha;
imageBytes[1] *= oldAlpha * newAlpha;
imageBytes[2] *= oldAlpha * newAlpha;
imageBytes[3] = newAlphaValue;
  • 如果文章對你有幫助,請點下“喜歡”吧,感謝!
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容