1

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;
}
Jeff
  • 2,818
  • 3
  • 29
  • 31
  • Take a look at this code, it works: Take a look at http://stackoverflow.com/questions/1260249/resizing-uiimages-pulled-from-the-camera-also-rotates-the-uiimage/1262395#1262395 – progrmr May 17 '10 at 03:16
  • The code in that other question produces a null value for me here: CGImageRef ref = CGBitmapContextCreateImage(bitmap); – Jeff May 17 '10 at 13:43
  • bitmap has a memory address and seems to have been created modified correctly so I don't know what's going on there. – Jeff May 17 '10 at 13:43
  • nevermind it is working ... it's my layout code that is crushing other stuff. Thanks! – Jeff May 17 '10 at 14:46

2 Answers2

0

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

drawnonward
  • 53,459
  • 16
  • 107
  • 112
  • 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 May 17 '10 at 01:09
  • 1
    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 May 17 '10 at 03:59
  • 1
    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 May 17 '10 at 06:50
0

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
  • 2,818
  • 3
  • 29
  • 31