Graphics Programs Reference
In-Depth Information
Creating and using keys
When an image is added to the store, it will be put into a dictionary under a unique key,
and the associated
BNRItem
object will be given that key. When the
De-
tailViewController
wants an image from the store, it will ask its
item
for the key
and search the dictionary for the image. Add a property to
BNRItem.h
to store the key.
@property (nonatomic, readonly, strong) NSDate *dateCreated;
@property (nonatomic, copy) NSString *imageKey;
Synthesize this new property in the implementation file.
@implementation BNRItem
@synthesize imageKey;
The image keys need to be unique in order for your dictionary to work. While there are
many ways to hack together a unique string, we're going to use the Cocoa Touch mechan-
ism for creating universally unique identifiers (UUIDs), also known as globally unique
identifiers (GUIDs). Objects of type
CFUUIDRef
represent a UUID and are generated us-
ing the time, a counter, and a hardware identifier, which is usually the MAC address of the
ethernet card.
Import
BNRImageStore.h
at the top of
DetailViewController.m
.
#import "DetailViewController.h"
#import "BNRItem.h"
#import "BNRImageStore.h"
In
DetailViewController.m
, update
imagePickerControl-
ler:didFinishPickingMediaWithInfo:
to generate a UUID when a new pic-
ture is taken.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Create a CFUUID object - it knows how to create unique identifier strings
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
The prefix
CF
means
CFUUIDRef
comes from the Core Foundation framework (and re-
member that the
Ref
suffix means that it is a pointer). Core Foundation is a collection of
C “classes” and functions. Core Foundation objects are created by calling a function that
begins with the type of object being created and contains the word
Create
(
CFUUIDCreate
). When creating a Core Foundation object, the first argument specifies