Arun
Arun

Reputation: 590

iOS - How to store capture image and display it in your app

i am creating an application which capture an image form iphone camera and display it in the created application i read some articles in ios developer library and written this code in .m file

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
           usingDelegate: (id <UIImagePickerControllerDelegate,
                               UINavigationControllerDelegate>) delegate {

if (([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil))
    return NO;


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

// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes =
    [UIImagePickerController availableMediaTypesForSourceType:
        UIImagePickerControllerSourceTypeCamera];

// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;

cameraUI.delegate = delegate;

[controller presentModalViewController: cameraUI animated: YES];
return YES;
}

also created a button that is linked with this code, It call this function but the control does not passes to the above function

- (IBAction) showCameraUI {
[self startCameraControllerFromViewController: self
                                usingDelegate: self];
}

And i also have no idea that this code will work or not

Do some one have any better idea. Thanks, Arun.

Upvotes: 1

Views: 3002

Answers (2)

Hiren
Hiren

Reputation: 12780

First assign delegate to it's source file

Then when you pressed your button implement this method open camera controller in your app

First of all create PopOverController for iPad because we cannot directly open ImagePicker Controller

EDIT: Added UIPopOverController

- (IBAction) showCameraUI {
     BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
     UIImagePickerController* picker = [[UIImagePickerController alloc] init];
     picker.delegate = self;
     picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;

     if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController=nil;
     }

     self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
      CGRect popoverRect = [self.view convertRect:[yourBtn frame]
                                   fromView:[yourBtn superview]];

     popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
     popoverRect.origin.x = popoverRect.origin.x;

     [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
      permittedArrowDirections:UIPopoverArrowDirectionAny
     animated:YES];

    }

And implement this delegate method for get the captured image

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *yourImageView = image;
    if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}

Implement this method if user cancel the controller

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
     if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}

- (void)pickerDone:(id)sender
{
     if (popoverController != nil) {
     [popoverController dismissPopoverAnimated:YES];
     self.popoverController=nil;
     }
}

Upvotes: 2

Ashvin
Ashvin

Reputation: 9027

You can use delegate: UIImagePickerControllerDelegate

Use bellow:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)mediaAndInfo

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

Upvotes: 0

Related Questions