Gold Solution with Camera Overlay and Editing


I created a new class called CrossHairView which leveraged on Challenge 6 Silver:

CrossHairView.h was straight generated code, so I’ll exclude that.


//  CrossHairView.m
//  Challenge 12

#import "CrossHairView.h"

@implementation CrossHairView

- (id)initWithFrame:(CGRect)frame
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]]; //Need this so you can see camera image
    return self;

- (void)drawRect:(CGRect)rect
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect bounds = [self bounds];
    CGPoint center;
    center.x= bounds.origin.x + bounds.size.width / 2.0;
    center.y= bounds.origin.y + bounds.size.height / 2.0;
    CGContextSetLineWidth(ctx, 2);
    [[UIColor redColor] setStroke]; // Or choose any other color
    CGContextMoveToPoint(ctx, center.x- 20, center.y);
    CGContextAddLineToPoint(ctx, center.x+ 20, center.y);
    CGContextMoveToPoint(ctx, center.x, center.y - 20);
    CGContextAddLineToPoint(ctx, center.x, center.y + 20);

// This next method is critical.  Without it, this view captures all input gestures and doesn't allow you to edit
// Adding the below line will allow the parent view to receive input
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    return NO;

And then you just need to add a few lines to takePicture

- (IBAction)takePicture:(id)sender
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    // If our device has a camera, we want to take a picture, otherwise, we
    // just pick from photo library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];

        // Gold Challenge Code     
        CrossHairView *chv = [[CrossHairView alloc] init];     
        [chv setFrame:CGRectMake(0, 0, [imagePicker view].bounds.size.width, [imagePicker view].bounds.size.height - 52)];
        [imagePicker setCameraOverlayView:chv];
        // Gold Challenge Code end
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [imagePicker setDelegate:self];
    [imagePicker setAllowsEditing:YES]; //Bronze Challenge
    // Place image picker on the screen
    [self presentViewController:imagePicker animated:YES completion:nil];


Nice catch on pointInside:withEvent:.


Thanks so much - I went the same route as you (drawing the view rather than using an image) and I was really struggling with how to stop it grabbing touch input.
Not sure I would have ever found pointInside by browsing the docs so you saved me!