• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions


  • Subject: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
  • From: Ray <email@hidden>
  • Date: Tue, 27 Mar 2012 09:38:26 +0200

OK, so I have this iOS 3.x code base that I need to update. I have the following code, to make a thumbnail image:

...
// let's say we have a UIImage *selectedImage with a size of 1000 x 1000
UIImage *newImage;
NSData *imageData;

UIGraphicsBeginImageContext(CGSizeMake(44.0, 44.0));
[selectedImage drawInRect:CGRectMake(0.0, 0.0, 44.0, 44.0)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageData = UIImagePNGRepresentation(newImage);
...

Code is simplified, just for illustrative purposes. I store imageData as a binary property (called "listImage") in a Core Data entity and use it as a thumbnail image in a UITableView, like so:

cell.imageView.image = [UIImage imageWithData:item.listImage];

It's small, so no problems. On both a normal and retina screen iPhone running iOS 4 this works OK, although we don't make use of the increased resolution, yet. In order to do that, I update the code with:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(44.0, 44.0), YES, 0);

Now, when I generate a new image on an iPhone with a retina screen, in which case the above UIGraphicsBeginImageContextWithOptions uses a scale of 2.0, the image in the table view shows too big. This is because [UIImage imageWithData:item.listImage] always returns a 1.0 scaled image. Now I can solve this by using something like:

UIImage *theImage = [UIImage imageWithData:listImage];
[UIImage imageWithCGImage:theImage.CGImage scale:theImage.size.width > 44.0 ? 2.0 : 1.0 orientation:theImage.imageOrientation];

But this seems kludgy and it's using programmer's knowledge, so to speak. I watched WWDC2010 session 134 "Optimize your iPhone App for the Retina Display" again, searched stackoverflow etc. but I can't find a more elegant solution. What would be a more thorough approach? Maybe it's staring me in the face but I don't see it...

Thanks for your answers,
- Ray.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
      • From: David Duncan <email@hidden>
    • Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
      • From: Roland King <email@hidden>
    • Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
      • From: Marco Tabini <email@hidden>
    • Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
      • From: Giacomo Tufano <email@hidden>
  • Prev by Date: Re: File types...
  • Next by Date: Re: How to throttle rate of NSInputStream?
  • Previous by thread: Re: File types...
  • Next by thread: Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
  • Index(es):
    • Date
    • Thread