Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use the built-in camera of the iPhone ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 582
    Comment on it

    To use the built-in camera of the iPhone consider the following example. In this example we will also see how to access the photo library and select a photo and use it.

    Create a single view application and assign a name to it. Then in the Main.Storyboard drag an UIImageView and two UIButtons. After that make IBOutlet of UIImageView and IBAction of the UIButtons.

    Now in the ViewController.m file implement the following code :-

    - (IBAction)btnOpenCamera:(id)sender {    // action on the UIButton to use Camera 
       
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        // if camera is not supported by the device then show the alert message 
    
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"ALERT MESSAGE" message:@"Device has no camera" preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction *dismissAction=[UIAlertAction actionWithTitle:@"Dismiss" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:dismissAction];
            [self presentViewController:alert animated:YES completion:nil];
            return;
        }
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:picker animated:YES completion:NULL];
    
    }
    
    - (IBAction)btnSelectFromPhotoLibrary:(id)sender{    // action on the UIButton to select image from Photo Library
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:picker animated:YES completion:NULL];
        
    }
    

    We need to implement the delegate methods of UIImagePickerController so for that we have to include UIImagePickerControllerDelegate protocol and also we are going to present the camera so, we have to implement the UINavigationControllerDelegate protocol as well.

    @interface ViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

    Now implement the delegate methods as :-

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        self.imgView.image = chosenImage;
        
        [picker dismissViewControllerAnimated:YES completion:NULL];
        
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        
        [picker dismissViewControllerAnimated:YES completion:NULL];
        
    }

    It’s done !

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: