How to create an image from unsigned char *

I am trying to create a new image from raw data in an unsigned char *. I have a bit per pixel from the saved data. I tried converting the raw data to NSData, but the result was zero,

unsigned char * bitmap = myData;

NSUInteger myImageDataLength = strlen ((const char *) bitmap);

NSData * d = [NSData dataWithBytes: bitmap length: myImageDataLength];

imgView.image = [UIImage imageWithData: d];

Then I tried to use the context to create the image as follows:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); int width = mWidth; int height = mHeight; size_t bpp = mBpp; size_t bpc = 8; size_t bpr = (width * 4); CGContextRef context = CGBitmapContextCreate(bitmap, width, height, 8, bpr, colorspace, kCGImageAlphaNone); CGImageRef cgImage = nil; if (context != nil) { cgImage = CGBitmapContextCreateImage (context); CGContextRelease(context); } CGColorSpaceRelease(colorspace); UIImage *newImage = [UIImage imageWithCGImage:cgImage]; 

I get an error message like <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 64 bytes/row. <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 64 bytes/row.

I tried to solve it by setting many values, but could not find the result. I googled on the Internet to find this solution, but everyone else creates an image from existing images, so they get bit values ​​for components and all that ... How can I create an image from this data ???

Original Image:

enter image description here

Resulting Image:

enter image description here

The resulting image must be distorted. (Expires just like fatBooth and another application)

+4
source share
2 answers

The following code creates an instance of UIImage from raw pixel data, assuming you have:

  • pixelData: raw pixel data
  • imageWidth, imageHeight: image sizes
  • scanWidth: number of bytes per scan line
  • bytesPerPixel: number of bytes used per pixel, usually 4

The resulting UIImage assigned to the UIImage variable.

 CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, pixelData, imageHeight * scanWidth, NULL); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, bytesPerPixel * 8, scanWidth, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); UIImage *uiImage = [UIImage imageWithCGImage:imageRef]; CGDataProviderRelease(provider); CGColorSpaceRelease(colorSpaceRef); CGImageRelease(imageRef); 
+5
source

Try the following: -

 CGRect screenBounds = [[UIScreen mainScreen] bounds]; GLuint *buffer; //point the buffer to memory location code that here accordingly int backingWidth = screenBounds.size.width; int backingHeight =screenBounds.size.height; NSInteger myDataLength = backingWidth * backingHeight * 4; CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData); const int bitsPerComponent = 8; const int bitsPerPixel = 4 * bitsPerComponent; const int bytesPerRow = 4 * backingWidth; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGImageRef imageRef = CGImageCreate(backingWidth,backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provider); UIImage *myImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); [uiimageviewobject setImage:myImage]; [self addSubview:uiimageviewobject]; 
0
source

Source: https://habr.com/ru/post/1369425/


All Articles