views:

369

answers:

2

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;
}
A: 

You cannot pass nil as the first argument of CGBitmapContextCreate. Allocate some space for the pixels. Think of the poor pixels.

drawnonward
hmm I read this in the documentation:"you can pass NULL if you do not care where the data is stored. This frees you from managing your own memory, which reduces memory leak issues."
Jeff
Where I see that quote, it starts "In Mac OS X v10.6 and later, " and I am not sure how that applies to iPhone. If it is working for you then good enough. I usually pass the same data to a CGImageRef and let it handle memory management, instead of copying the pixels a second time with CGBitmapContextCreateImage.
drawnonward
If you browse the sample applications for iPhone, you will find at least one with NULL as first parameter: http://developer.apple.com/iphone/library/samplecode/Reflection/Introduction/Intro.html
Laurent Etiemble
A: 

So it appears I was using images that were TOO big. Essentially I was trying to scale by a magnitude of 10x which appears to be too much. Anyways, that's that.

Jeff