Search code examples
iphoneobjective-cios4uiimagepickercontroller

iphone sdk how to take a picture from camera and show the image in same view?


I will create a button and imageview .I will create IBAction for button to open UIImagepicker to take the photo.I need after that The image need to show in the imageview.How to get this.Can any one help me please.


Solution

  • Call this IBAction method on a button click. It will open UIImagePickerController to capture an image from the camera.

    -(IBAction)openCamera
    {
        @try 
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
            picker.delegate = self; 
    
            [self presentModalViewController:picker animated:YES];
            [picker release];
        }
        @catch (NSException *exception) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available  " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
    

    This delegate will be called when you are finished taking the image. Store that image in your UIImageView.

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
        [picker dismissModalViewControllerAnimated:YES];
        self.imageView.image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    }