Posted By

aahmed753 on 07/19/09


Tagged

iphone UIImage


Versions (?)

UIImage resize


 / Published in: C
 

  1. UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
  2. {
  3. CGImageRef imageRef = [inImage CGImage];
  4. CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
  5.  
  6. // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
  7. // see Supported Pixel Formats in the Quartz 2D Programming Guide
  8. // Creating a Bitmap Graphics Context section
  9. // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
  10. // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
  11. // The images on input here are likely to be png or jpeg files
  12. if (alphaInfo == kCGImageAlphaNone)
  13. alphaInfo = kCGImageAlphaNoneSkipLast;
  14.  
  15. // Build a bitmap context that's the size of the thumbRect
  16. CGContextRef bitmap = CGBitmapContextCreate(
  17. NULL,
  18. thumbRect.size.width, // width
  19. thumbRect.size.height, // height
  20. CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
  21. 4 * thumbRect.size.width, // rowbytes
  22. CGImageGetColorSpace(imageRef),
  23. alphaInfo
  24. );
  25.  
  26. // Draw into the context, this scales the image
  27. CGContextDrawImage(bitmap, thumbRect, imageRef);
  28.  
  29. // Get an image from the context and a UIImage
  30. CGImageRef ref = CGBitmapContextCreateImage(bitmap);
  31. UIImage* result = [UIImage imageWithCGImage:ref];
  32.  
  33. CGContextRelease(bitmap); // ok if NULL
  34. CGImageRelease(ref);
  35.  
  36. return result;
  37. }

Report this snippet  

You need to login to post a comment.