Graphics Programs Reference
In-Depth Information
[imageView setImage:imageToDisplay];
} else {
// Clear the imageView
[imageView setImage:nil];
}
}
Notice that if no image exists in the image store for that key (or there is no key for that
item), the pointer to the image will be
nil
. When the image is
nil
, the
UIImageView
just won't display an image.
Build and run the application. Create a
BNRItem
and select it from the
UITableView
.
Then, tap the camera button and take a picture. The image will appear as it should.
There is another detail to take care of: if you select a new image for a
BNRItem
, the old
one will still be in the
BNRImageStore
. At the top of
imagePickerControl-
ler:didFinishPickingMediaWithInfo:
in
DetailViewController.m
,
add some code to tell the
BNRImageStore
to remove the old image.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *oldKey = [item imageKey];
// Did the item already have an image?
if (oldKey) {
// Delete the old image
[[BNRImageStore sharedStore] deleteImageForKey:oldKey];
}
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
Build and run the application again. The behavior should remain the same, but the
memory benefits are significant.
Dismissing the keyboard
When the keyboard appears on the screen in the item detail view, it obscures
De-
tailViewController
's
imageView
. This is annoying when you're trying to see an
image, so you're going to implement the delegate method
tex-
tFieldShouldReturn:
to have the text field resign its first responder status to dis-
miss the keyboard when the return key is tapped. (This is why you hooked up the
del-
egate
outlets earlier.) But first, in
DetailViewController.h
, have
De-
tailViewController
conform to the
UITextFieldDelegate
protocol.
@interface DetailViewController : UIViewController