Why not alway create a CGBitmapContext of the desired size and in a supported pixel format (see <http://developer.apple.com/mac/library/qa/qa2001/qa1037.html >), then call CGContextDrawImage to draw the CGImage into the context? Then you're always controlling the parameters.

CGRect bounds = CGRectMake(0, 0, scaleFactorX * image.size.width, scaleFactorY * image.size.height);
context = CGBitmapContextCreate(NULL,
                                bounds.size.width,
                                bounds.size.height,
                                8,
                                4 * bounds.size.width,
                                CGColorSpaceCreateDeviceRGB(),                  
        kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, bounds, image.CGImage);


On Nov 1, 2009, at 8:45 AM, thatsanicehatyouh...@mac.com wrote:

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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to