Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
- Subject: Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
- From: Roland King <email@hidden>
- Date: Tue, 27 Mar 2012 22:21:06 +0800
UIImageView's scale automatically so just setting the frame of the UIImageView containing the UIImage to ( x, y, 44, 44 ), possibly after you load the image into it, will just work whatever size the image actually is on whatever type of display you are using.
On Mar 27, 2012, at 3:38 PM, Ray wrote:
> 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
_______________________________________________
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