Bulletproof way to create a new CGBitmapContext from an existing image?
Bulletproof way to create a new CGBitmapContext from an existing image?
- Subject: Bulletproof way to create a new CGBitmapContext from an existing image?
- From: email@hidden
- Date: Sun, 01 Nov 2009 11:45:16 -0500
Hi,
I have some code where I'm downloading images from the internet.
They're probably all going to be jpegs, but I'm not making that
assumption in my code. I have no control over the images. I'm creating
a CGBitmapContext to manipulate them. Further, I'm scaling the image
(could be up or down depending on the original size) by creating the
context with the size I want and drawing the original image into that.
I'm trying to write code to avoid messages such as this:
<Error>: CGBitmapContextCreate: unsupported parameter combination: 8
integer bits/component; 16 bits/pixel; 1-component colorspace;
kCGImageAlphaPremultipliedFirst; 352 bytes/row.
I only rarely get these now, but I want to make that "never". I'm
pasting my code below that I have so far - can anyone suggest
something better? (I'm working on an iPhone if it makes any difference.)
Cheers,
Demitri
size_t bitsPerComponent = CGImageGetBitsPerComponent(image.CGImage);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
// The value returned by CGImageGetBytesPerRow might be smaller than
the final
// image if we are scaling to a larger size.
float contextWidth = (float)ceil((double)scaleX*image.size.width) * 4.0;
int bytesPerRow = (CGImageGetBytesPerRow(image.CGImage) >
contextWidth) ? CGImageGetBytesPerRow(image.CGImage)
: contextWidth;
context = CGBitmapContextCreate(NULL, // let the OS manage the memory
for me
// image dimensions (desired width, desired height)
scaleFactorX*image.size.width, scaleFactorY*image.size.height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden