Graphics Programs Reference
In-Depth Information
projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;
[image drawInRect:projectRect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
[self setThumbnail:smallImage];
NSData *data = UIImagePNGRepresentation(smallImage);
[self setThumbnailData:data];
UIGraphicsEndImageContext();
}
The
thumbnail
attribute is not going to be saved - it is a transient attribute. You'll need
to update
thumbnail
from the
thumbnailData
when the object first emerges from
the filesystem. When
Homepwner
used keyed archiving, we did this in
initWithCoder:
. Now that we're using Core Data, objects are initialized by another
Core Data object, which you will meet in a moment. Thus, you do not implement
init
methods for
NSManagedObject
subclasses. Instead, to configure an object after it has
been created, you override the method
awakeFromFetch
. Implement
awakeFromFetch
in
BNRItem.m
to set the thumbnail from the
thumbnailData
(which is saved).
- (void)awakeFromFetch
{
[super awakeFromFetch];
UIImage *tn = [UIImage imageWithData:[self thumbnailData]];
[self setPrimitiveValue:tn forKey:@"thumbnail"];
}
This adds the extra behavior of
BNRItem
's old implementation of
initWithCoder:
.
Of course, when you first launch an application, there are no saved
BNRItem
s or
BNRAssetType
s. When the user creates a new
BNRItem
instance, it will be added to
the database. When objects are added to the database, they are sent the message
awakeFromInsert
. Here is where you will set the
dateCreated
instance variable
of a
BNRItem
. Implement
awakeFromInsert
in
BNRItem.m
.
- (void)awakeFromInsert
{
[super awakeFromInsert];
NSTimeInterval t = [[NSDate date] timeIntervalSinceReferenceDate];
[self setDateCreated:t];
}
This adds the extra behavior of
BNRItem
's old designated initializer. Build the applica-
tion to check for syntax errors, but do not run it.