Graphics Programs Reference
In-Depth Information
Image Manipulation
To display an image within a cell, you could just resize the large image of the item from the
image store. However, doing so would incur a performance penalty because a large number
of bytes would need to be read, filtered, and resized to fit within the cell. A better idea is to
create and use a thumbnail of the image instead.
To create a thumbnail of a
BNRItem
image, you are going to draw a scaled-down version
of the full image to an offscreen context and keep a pointer to that new image inside a
BNRItem
instance. You also need a place to store this thumbnail image so that it can be re-
loaded when the application launches again.
flushed if necessary. However, the thumbnail images will be small enough that we can
archive them with the other
BNRItem
instance variables.
Big problem, though: the thumbnail will be an instance of
UIImage
.
UIImage
doesn't
conform to the
NSCoding
protocol, so you can't encode the thumbnail directly in an
NSCoder
. What you can do is encode the thumbnail as data (PNG format) and wrap it in
an
NSData
object, which does conform to
NSCoding
.
Open
BNRItem.h
. Declare two new properties: a
UIImage
and an
NSData
. You will
also want a method to turn a full-sized image into a thumbnail.
@property (nonatomic, copy) NSString *imageKey;
@property (nonatomic, strong) UIImage *thumbnail;
@property (nonatomic, strong) NSData *thumbnailData;
- (void)setThumbnailDataFromImage:(UIImage *)image;
@end
Synthesize these properties in
BNRItem.m
. You will eventually override the default setter
and getter methods for these properties, but synthesizing automatically generates the two
instances variables you need.
@implementation BNRItem
@synthesize thumbnail, thumbnailData;
When an image is chosen for this
BNRItem
, you will give that image to the
BNRItem
. It
will chop it down to a much smaller size and then keep that smaller-sized image as its
thumbnail
. It will also create an
NSData
object that is the PNG representation of that