0

I have a problem with PickerDidFinishPicking, it should be called after I got an image from my iphone camera but it is not called here is my code:

first here is mt h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

@end

and here is the m file:

#import "CameraOverlay.h"

@interface CameraOverlay ()
@property (nonatomic) UIImagePickerController *imagePickerController;
@end

@implementation CameraOverlay
@synthesize imagePicker;

- (void)viewDidLoad {
    [super viewDidLoad];


   UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(0, 0, 200, 60);
    myButton.center = self.view.center;
   [myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
   [myButton setTitle:@"Image Picker" forState:UIControlStateNormal];

   [self.view addSubview:myButton];
    [self buttonPress:self];
}

- (void)buttonPress:(id)sender {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // alert the user that the camera can't be accessed
        UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Unable to access the camera!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
        [noCameraAlert show];

    } else {



        // prepare imagePicker view
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.showsCameraControls = NO;

        // create view for overlay
        CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
        UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];

        // prepare the image to overlay
        //UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"theKing"]];
        //overlayImage.center = overlayView.center;
        //overlayImage.alpha = 0.5;

        // prepare toolbar for overlay
        UIToolbar *overlayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 600, overlayView.frame.size.width, 40)];
        overlayToolbar.center = CGPointMake(overlayView.center.x, overlayView.frame.size.height - 20);
        overlayToolbar.barStyle = UIBarStyleBlack;

        UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(takePictureButtonPressed:)];
        UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        flexibleBarSpace.width = 1000;
        UIBarButtonItem *startStopButton = [[UIBarButtonItem alloc] initWithTitle:@"Snap" style:UIBarButtonItemStyleBordered target:self action:@selector(startStopButtonPressed:)];
        UIBarButtonItem *timedButton = [[UIBarButtonItem alloc]initWithTitle:@"Timed" style:UIBarButtonItemStyleBordered target:self action: @selector(timedButtonPressed:)];
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action: @selector(cancelButtonPressed:)];

        overlayToolbar.items = [NSArray arrayWithObjects:takePictureButton, flexibleBarSpace, startStopButton, timedButton, cancelButton, nil];


       // [overlayView addSubview:overlayImage];
        [overlayView addSubview:overlayToolbar];

        // add the image as the overlay
        imagePicker.cameraOverlayView =overlayView;

        // display imagePicker
        [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
    }
}
-(void)SavePhoto:(UIImage *) imageTosave{
    NSData *pngData = UIImagePNGRepresentation(imageTosave);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"/CapturesPhotos"];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:zone];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString* namee = [NSString stringWithFormat:@"/IMG_%@.png",[formatter stringFromDate:date]];
    NSString *filePath = [dataPath stringByAppendingPathComponent:namee]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file
}
#pragma mark - UIImagePickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo {

    NSLog(@"imagecontroller");
    // determine if the user selected or took a new photo
    UIImage *selectedImage =  [editingInfo valueForKey:UIImagePickerControllerOriginalImage];

    [self SavePhoto:selectedImage];

    // TODO: Do something with selectedImage (put it in a UIImageView

    // dismiss the imagePicker
    [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIBarButton Selectors
- (void)takePictureButtonPressed:(id)sender {
    NSLog(@"takePictureButtonPressed...");
    [self.imagePickerController takePicture];
        // TODO: take picture!
}
- (void)startStopButtonPressed:(id)sender {
    NSLog(@"startStopButtonPressed...");
    // TODO: make this do something
}
- (void)timedButtonPressed:(id)sender {
    NSLog(@"timedButtonPressed...");
    // TODO: implement timer before calling takePictureButtonPressed
}
- (void)cancelButtonPressed:(id)sender {
    NSLog(@"cancelButtonPressed");
    [self.navigationController dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
@end

when I press the button to get an image from the camera the picker function is not called why is that happening ?! I first press the button that is connected to [self.imagePickerController takePicture] then the picker should be called and after that the picture should be saved by calling the method -(void)SavePhoto:(UIImage *) imageTosave.

thanks in advance

3
  • can you post your .h?
    – Ilario
    Jan 9, 2014 at 14:42
  • Is - (void)takePictureButtonPressed:(id)sender being called? Jan 9, 2014 at 14:47
  • you do a @property of imagePickerController, and a synthesize imagePicker
    – Ilario
    Jan 9, 2014 at 14:49

2 Answers 2

0

Make sure that you have added UIImagePickerControllerDelegate category in the class declaration.

0
0

Pressing that button [self buttonPress:self]; in viewDidLoad is much too early.

Step one: change the code so it compiles, change everything that says imagePicker to self.imagePickerController.

Step two: comment out the self buttonPress line. If you'd like to trigger the picker early, do the VC transition in viewDidAppear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.