Conversion between iOS UIImage and Bytes Pixel

  • IOS video image development can not avoid the use of the conversion between UIImage and Bytes Pixel. Bytes pixel can be understood as a picture of all the pixels in the two-dimensional array.
  • This article will be the picture UIImage and BGRA between pixel conversion as an example to explain.
Conversion between iOS UIImage and Bytes Pixel
UIImage iOS conversion with Pixel
  • First of all, a simple look at the context of the image generated by the CGBitmapContextCreate method prototype and parameters.
* @param data / * * to render the memory address mapping. The size of the memory block is at least (bytesPerRow*height) bytes width bitmap * @param width, in pixels, @param * height bitmap height, in pixels, each pixel @param bitsPerComponent * component in memory number. For example, for 32 bit pixel format and RGB color space, you should set this value to 8 * @param bytesPerRow bitmap for each row of memory bits * @param space bitmap * @param color space using the context bitmapInfo specifies whether the bitmap contains the alpha channel, alpha channel in the relative position of the pixel, the pixel component is a string of plastic or floating-point information. * / CGContextRef CGBitmapContextCreate (void *data, size_t width, size_t height, size_t bitsPerComponent, size_t bytesPerRow, CGColorSpaceRef colorspace, CGBitmapInfo bitmapInfo);
  • UIImage object is converted to BGRA
(unsigned * char) - pixelBRGABytesFromImage: (UIImage *) image [self {return pixelBRGABytesFromImageRef:image.CGImage];} - (unsigned * char) pixelBRGABytesFromImageRef: (CGImageRef imageRef) {NSUInteger iWidth = CGImageGetWidth (imageRef); NSUInteger iHeight (imageRef) NSUInteger = CGImageGetHeight; 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;}
  • Usage method
UIImage *image [UIImage imageNamed:@ = "pict1.png"]; unsigned char *imageBytes = [self pixelBRGABytesFromImage:image]; / / Note: don't forget to release the memory of malloc free (imageBytes);
  • BGRA convert to UIImage object
- (UIImage) imageFromBRGABytes: (unsigned * char) imageBytes imageSize: (CGSize) imageSize CGImageRef [self imageRefFromBGRABytes:imageBytes imageSize:imageSize] {imageRef = UIImage; *image = [UIImage imageWithCGImage:imageRef]; CGImageRelease (imageRef); return image;} - (CGImageRef) imageRefFromBGRABytes: (unsigned * char) imageBytes imageSize: (CGSize) imageSize (colorSpace) {CGColorSpaceRef = CGColorSpaceCreateDeviceRGB; CGContextRef context = CGBitmapContextCreate (imageBytes, imageSize.width, imageSize.height, 8, imageSize.wid Th * 4, colorSpace, kCGImageAlphaPremultipliedFirst) CGImageRef kCGBitmapByteOrder32Little |; imageRef = CGBitmapContextCreateImage (context); CGContextRelease (context); CGColorSpaceRelease (colorSpace); return imageRef;}
  • Usage method
UIImage *image = [self imageFromBRGABytes:imageBytes imageSize:imageSize]; NSLog (image =% @, @ image);
  • Note that
    CGImageRef is a struct pointer in the Core Graphics framework. Prototype: typedef struct *CGImageRef; even in ARC also need to manually release the object, call CGImageRelease or CFRelease release CGImage.
  • extend
  • UIImage converted to Gray pixel gray map method:
    will get the CGColorSpaceRef method to modify the CGColorSpaceCreateDeviceGray (), and iBytesPerPixel 4 to 1. Originally used 4byte BGRA to describe the picture, now only use 1byte to describe the gray.
  • Example of the Alpha option is kCGImageAlphaPremultipliedFirst, the option of the premultiplied represents the image of the BGR value is multiplied by the Alpha value, so want to get the pixel value of the BGR. CGFloat oldAlpha / imageBytes[3] = 255; CGFloat = newAlpha (CGFloat) newAlphaValue / 255 oldAlpha * newAlpha; imageBytes[0] = imageBytes[1]; oldAlpha = newAlpha * oldAlpha * newAlpha * = imageBytes[2]; imageBytes[3] = newAlphaValue;
  • If the article is helpful to you, please click “like”, thank you!