2

I have the problem with creating context..

I have seen lots of answer but none of this working for me.

I have the following code.

CGSize pixelSize = src_img.size;
pixelSize.width = src_img.scale* src_img.size.width;
pixelSize.height = src_img.scale*src_img.size.height;

size_t d_bytesPerRow = pixelSize.width * 4;
unsigned char *imgData = (unsigned char*)malloc(pixelSize.height * d_bytesPerRow);
CGImageRef imageRef = src_img.CGImage;

CGContextRef context =  CGBitmapContextCreate(imgData,
                                              (size_t)pixelSize.width, (size_t)pixelSize.height, 8, d_bytesPerRow,
                                              CGImageGetColorSpace(imageRef), kCGImageAlphaNone);

In console shows me the error

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 1754 bytes/row.

I don't know what to do. Please help

user3772344
  • 153
  • 2
  • 10

2 Answers2

12

You are not specifying one of the supported pixel formats. The image's colorspace is apparently an RGB color space. You're specifying kCGImageAlphaNone. Together, that means 3 components per pixel and no padding. Combined with the 8 bits per component that you specified gives 24 bits per pixel.

None of the supported pixel formats allow for 24 bits per pixel. That's another way of saying that none of them supports RGB with kCGImageAlphaNone. If you don't have or want alpha, you need to use either kCGImageAlphaNoneSkipFirst or kCGImageAlphaNoneSkipLast, which means a byte of padding in each pixel bumping it up to 32 bits per pixel.

Also, you're almost always better off specifying NULL for the data parameter and 0 for the bytesPerRow parameter. The former makes memory management much easier. The latter is not only easier, but allows the system to optimize the buffer's alignment.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
2

Since the error messages says “1754 bytes/row”, and you're computing the bytes/row as 4 * src_img.size.width, I can solve for src_img.size.width = 1754 / 4 = 438.5. You can't have half a pixel in your bitmap.

I suspect you have a UIImage with a scale of 2. A UIImages dimensions are measured in points, but a CGBitmapContext's dimensions are measured in pixels. To convert the UIImage's dimensions to pixels, you need to multiply by its scale.

CGSize pixelSize = src_img.size;
pixelSize.width *= src_img.scale;
pixelSize.height *= src_image.scale;

size_t d_bytesPerRow = pixelSize.width * 4;
unsigned char *imgData = (unsigned char*)malloc(pixelSize.height * d_bytesPerRow);
CGImageRef imageRef = src_img.CGImage;

CGContextRef context =  CGBitmapContextCreate(imgData,
    (size_t)pixelSize.width, (size_t)pixelSize.height, 8, d_bytesPerRow,
    CGImageGetColorSpace(imageRef), kCGImageAlphaNone);

You will also need to take the scale into account if you plan to draw the image into the context, perhaps by using CGContextScaleCTM.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848