CGBitmapContextCreate issue while trying to resize images
Posted
by Jeff
on Stack Overflow
See other posts from Stack Overflow
or by Jeff
Published on 2010-05-17T00:23:01Z
Indexed on
2010/05/17
0:30 UTC
Read the original article
Hit count: 723
Hello! I'm running into an issue when I try to create a CGContextRef while attempting to resize some images:
There are the errors
Sun May 16 20:07:18 new-host.home app [7406] <Error>: Unable to create bitmap delegate device
Sun May 16 20:07:18 new-host.home app [7406] <Error>: createBitmapContext: failed to create delegate.
My code looks like this
- (UIImage*) resizeImage:(UIImage*)originalImage withSize:(CGSize)newSize {
CGSize originalSize = originalImage.size;
CGFloat originalAspectRatio = originalSize.width / originalSize.height;
CGImageRef cgImage = nil;
int bitmapWidth = newSize.width;
int bitmapHeight = newSize.height;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, bitmapWidth, bitmapHeight, 8, bitmapWidth * 4, colorspace, kCGImageAlphaPremultipliedLast);
if (context != nil) {
// Flip the coordinate system
//CGContextScaleCTM(context, 1.0, -1.0);
//CGContextTranslateCTM(context, 0.0, -bitmapHeight);
// Black background
CGRect rect = CGRectMake(0, 0, bitmapWidth, bitmapHeight);
CGContextSetRGBFillColor (context, 0, 0, 0, 1);
CGContextFillRect (context, rect);
// Resize box to maintain aspect ratio
if (originalAspectRatio < 1.0) {
rect.origin.y += (rect.size.height - rect.size.width / originalAspectRatio) * 0.5;
rect.size.height = rect.size.width / originalAspectRatio;
} else {
rect.origin.x += (rect.size.width - rect.size.height * originalAspectRatio) * 0.5;
rect.size.width = rect.size.height * originalAspectRatio;
}
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
// Draw image
CGContextDrawImage (context, rect, [originalImage CGImage]);
// Get image
cgImage = CGBitmapContextCreateImage (context);
// Release context
CGContextRelease(context);
}
CGColorSpaceRelease(colorspace);
UIImage *result = [UIImage imageWithCGImage:cgImage];
CGImageRelease (cgImage);
return result;
}
© Stack Overflow or respective owner