1

I am trying to get a image that is take from a camera and place than taken image into a UIImageView (without using the camera roll - just directly from the camera to the UIImageView). I have tried my best for weeks to try to work around this with no success. Does anyone have an idea why this does not work?

FYI, What have I been doing?

  1. Using this code below
  2. Press button to open camera UI
  3. Press capture button to take image
  4. Press the "use photo" button to select photo
  5. Expecting the photo to be placed in the UIImageView on the screen (But it doesn't...)
  6. Feelings of frustration

I'd be grateful if a wise coder can help me figure this one out. I feel like I'm doing everything that I'm reading others are telling me to do. Any code typed corrections would be extremely helpful. Thank you!

 - (IBAction)cameraButtonPushed:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
    //todo if device has no camera

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable To Locate Camera" message:@"No Camera found." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];
}

else {

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.delegate = delegate;
    cameraUI.allowsEditing = YES;
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

    cameraUI.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

    [self presentViewController:cameraUI animated:YES completion:^{

    }];

    }

}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[picker dismissViewControllerAnimated:YES completion:^{

    UIImage *takenImage = [info valueForKey:UIImagePickerControllerEditedImage];

    [self.navigationController pushViewController:self animated:YES];

    image = [[UIImageView alloc] initWithImage:takenImage];

}];

}
Tom Testicool
  • 563
  • 7
  • 26
  • What is the `image` variable in `imagePickerController:didFinishPickingMediaWithInfo:`? Is it an `IBOutlet`? – Austin May 27 '14 at 20:44
  • Yes it is. @property (strong, nonatomic) IBOutlet UIImageView *image; – Tom Testicool May 27 '14 at 20:46
  • Short answer: Use AVFramework rather than UIImagePickerController. – Caleb May 27 '14 at 20:48
  • @TomTesticool : What's your reason for doing `[self.navigationController pushViewController:self animated:YES];`? I don't think you need it. Plus... check if you have connected `image` object via the IB or try `[self.view addSubview:image];` after `image = [[UIImageView alloc] initWithImage:takenImage];` – staticVoidMan May 27 '14 at 20:52
  • I don't mind using whichever way I need to but just confused as to why AVFramework is the one to use for this. I don't need nor want video capture but if that is what I have to do to get an image that is what I will use. I'll keep plugging away at this until I get it to work. Thanks for the direction! – Tom Testicool May 27 '14 at 20:53
  • @staticVoidMan I am not exactly sure either. Some dude on the internet I read told some other guy that was what his problem was so I put it in my code too. That's how desperate I am to make this work. – Tom Testicool May 27 '14 at 20:55
  • @TomTesticool My impression after reading through your question the first time was that you wanted to avoid the image picker UI. AVFramework lets you pull data more or less straight from the camera without the camera interface. Realized maybe that's not what you're after once I read it a second and third time. – Caleb May 27 '14 at 20:55
  • 1
    The `didFinishPickingMediaWithInfo` is being called? wouldn't be correct to set the delegate to `self` on `cameraUI.delegate = delegate`? – Douglas Ferreira May 27 '14 at 20:56
  • @Caleb I'll try out the AVFramework like you say. But yeah the whole point of my project was that users would take pictures *from the camera itself* to place into the UIImageView directly. The idea being they have to 'prove' the instance has happened by taking a picture on the spot. Not using previously taken photos inputted through the camera roll. – Tom Testicool May 27 '14 at 20:58
  • Thanks for all of the suggestions guys! And so quickly you all got back to me too. I will try some of these tricks when I get home but it's 5 o'clock and I'm heading out of here. Everyone have a blessed day! – Tom Testicool May 27 '14 at 21:06
  • Just to update, everything I've tried has failed. If one of you guys can figure this thing out for me it would be appreciated greatly. I'm sure I'm not the only one having this problem. – Tom Testicool May 28 '14 at 02:53
  • @TomTesticool : **[1]** Set a breakpoint on `-didFinishPickingMediaWithInfo:` method. **[2]** Mention any errors (_if any_) **[3]** Mention what you **can** do currently (_with the current code_) – staticVoidMan May 28 '14 at 07:00

3 Answers3

3

try this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES
                               completion:^{
                                   UIImage *takenImage = [info valueForKey:UIImagePickerControllerEditedImage];

                                   image = [[UIImageView alloc] initWithImage:takenImage];
                                   [self.view addSubview:image];

                                   //or if object is connected properly via IB then simply
                                   //[image setImage:takenImage];
                               }];
}

And as per the keen observation by @DouglasFerreira

cameraUI.delegate = delegate;

should be

cameraUI.delegate = self;
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • If `image` was connected from Interface Builder, as OP says it is, then recreating the image view is unnecessary. – Austin May 27 '14 at 21:49
  • @Austin : yes, true but the question didn't specify it & so i went for a generic solution that should work in both cases (with or without `IBOutlet` unless there are other issues to be fixed) – staticVoidMan May 28 '14 at 07:44
2

If image is an IBOutlet, you shouldn't have to initialize it : it will be done by your storyboard or your XIB. Try just setting the image :

[image setImage: takenImage];
Emilie
  • 2,413
  • 13
  • 12
0

You could use the following to accomplish your task but make sure you make your view controller delegate to UIImagePickerControllerDelegate, UINavigationControllerDelegate

- (IBAction)takePhoto:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];
}



- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];
}
Sumit Kumar Saha
  • 799
  • 1
  • 12
  • 25