Graphics Reference
In-Depth Information
In light of this, JPEG would have been a better choice for our image carousel app. If we had
used JPEG rather than PNG, some of the threaded loading and caching tricks may not have
been necessary at all.
Unfortunately, it's not always possible to use JPEG images. If the image requires
transparency or has fine details that compress poorly using the JPEG algorithm, you have
no choice but to use a different format. Apple has specifically optimized the PNG and
JPEG loading code paths for iOS, so these are generally the preferred formats. That said,
there are some other options are available that can be useful in certain circumstances.
Hybrid Images
For images containing alpha transparency, it's possible to get the best of both worlds by
using a PNG to compress the alpha channel and a JPEG to compress the RGB part of the
image and then combine them after loading. This plays to the strengths of each format, and
results in an image with close-to-PNG quality and close-to-JPEG file size and loading
performance. Listing 14.7 shows the code to load a separate color and mask image and then
combine them at runtime.
Listing 14.7 Creating a Hybrid Image from a PNG Mask and a JPEG
#import "ViewController.h"
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- ( void )viewDidLoad
{
[ super viewDidLoad ];
//load color image
UIImage *image = [ UIImage imageNamed : @"Snowman.jpg" ];
//load mask image
UIImage *mask = [ UIImage imageNamed : @"SnowmanMask.png" ];
//convert mask to correct format
CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray ();
CGImageRef maskRef =
CGImageCreateCopyWithColorSpace (mask. CGImage , graySpace);
CGColorSpaceRelease (graySpace);
Search WWH ::




Custom Search