Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
- Subject: Re: Question about UIImage, scaling, and UIGraphicsBeginImageContextWithOptions
- From: Marco Tabini <email@hidden>
- Date: Tue, 27 Mar 2012 07:47:35 -0400
Hi Ray—
> 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...
UIImage does not represent the actual bits of an image, but, rather, provides the logical metadata wrapper of an underlying image object (in this case, a CGImage) to store information like scale and orientation. When you save to a file format that supports metadata, like JPEG, some of the metadata is stored there, while other is derived from the file name. So, for example, if you were to save a PNG file to disk with an @2x suffix, upon loading it with -imageNamed: UIImage would automatically know to set up the resulting object as a 2x scale image.
When the filename convention is not available, that metadata needs to be saved somewhere—in your case, you derive it from the file size, which, as you mention, is not very portable. I would have saved the scale in a separate Core Object property, or avoided saving the image in a data store to start with, and used a file cache, in which case UIImage is a bit more self-reliant.
In practice, I would want to look at more significant problems with your approach in a real-life deployment scenario. For example, what happens when your Core Data store is transferred from a non-Retina device to a Retina device? This could happen in a variety of scenarios: the user could start using your app on a non-Retina device (say, an iPad 2), then buy a new Retina device (a New iPad), back up the old one and restore on the new one. Now, your images are cached at 1x scale, but the device wants to run at 2x. Or, perhaps, you may, at some point, want to sync your data store using iCloud, causing some images to be generated on Retina devices, and some others at 1x scale.
Conversely, if your data is only ever used for caching purposes and is stored in the Caches directory, then this is a non-problem. Since the images are both going to be generated and used on the very same device, you don't need to derive the scale of the image from its size—you can just use the scale of your main screen both when creating and recreating your UIImage objects.
Then again, if you move your images to individual files and save them with the appropriate @2x suffix, this whole problem goes away altogether—so, perhaps, that may be a better solution.
Cheers,
—Mt.
_______________________________________________
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