У меня возникает проблема, когда я пытаюсь создать CGContextRef при попытке изменить размеры некоторых изображений:
Есть ошибки
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.
Мой код выглядит так
- (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;
}