Chapter 11 Solutions - Bronze / Silver / Gold Challenges


#1

OUTPUT:

GENERAL

CROSSHAIRS

I configured the detailed view image with a placeholder image via the viewWillAppear method. The delete UIButton is set to disabled unless a matching image is found in the image store for the item in question

[code] // Define image for the item per what is stored in the image store (dictionary)
UIImage *image = [[ZGCImageStore sharedStore] imageForKey:self.item.itemKey];
if (image) {
// Configure dictionary image
self.imageView.image = image;

    // enable trash button
    self.trashButton.enabled = YES;
    
} else {
    // configure paceholder image
    self.imageView.image = [UIImage imageNamed:@"no_image.jpg"];
    // disable trash button
    self.trashButton.enabled = NO;
    
}[/code]

BRONZE CHALLENGE - ALLOW EDITING / HAND OVER EDITED VERSION

1.- Set editing property for image picker in the takePicture action method

// Allow Editing imagePicker.allowsEditing = YES;

2.- Pull the edited version of the image in the ‘didFinishPickingMediaWithInfo’ delegate method

// Get picked image from info dictionary UIImage *image = info[UIImagePickerControllerEditedImage]; // keys in this dict. are defined constants

SILVER CHALLENGE - ADD A DELETE PICTURE BUTTON
[code]- (IBAction)clearPicture:(id)sender {

// Remove from image shared store
[[ZGCImageStore sharedStore] deleteImageForKey:self.item.itemKey];

// Update imageview image
self.imageView.image = [UIImage imageNamed:@"no_image.jpg"];

// Disable Trash button
self.trashButton.enabled = NO;

}[/code]

GOLD CHALLENGE - ADD CROSSHAIRS VIA CAMERA OVERLAY

1.- Load an overLay view for when source type is camera, in the takePicture action method

[code]
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { // <- convinience +method
// set source to camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

     // Load custom view
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil]; // <- load XIB for my OverlayView - view has clear background
    
    imagePicker.cameraOverlayView = self.overlayView;[/code]

2.- Register our controller as an observer with the default notification center so that we know when a photo has been taken and we can hide the overlay view (not necessary but keeps crosshair away when previewing the image

- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideOverlay:) name:@"_UIImagePickerControllerUserDidCaptureItem" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideOverlay:) name:@"_UIImagePickerControllerUserDidRejectItem" object:nil]; }

3.- Implement the hideOverlay selector called by the notificaiton center

[code]- (void)hideOverlay:(NSNotification *)note {

// Log the notification
NSLog(@"%@", note.name);

if ([note.name isEqualToString: @"_UIImagePickerControllerUserDidCaptureItem"]) {
    self.overlayView.hidden = YES;
} else {
    self.overlayView.hidden = NO;
}

}[/code]


#2

Thanks for your solutions.But i still can’t figure out how the notification works.Why it prints “_UIImagePickerControllerUserDidCaptureItem” when i’ve taken a photo?And why it prints “_UIImagePickerControllerUserDidRejectItem? when i press the “retake” button? How does the selector know which notification to receive?Could you explain it to me?Thanks!