Image Picker

Here we are starting ‘ImagePicker’ for picked image from gallery and camera.

See first video for learning easily. 



Step 1-First start X-code and open ’Single View Application’ show bellow picture.


Step 2-Now give product name ‘ImagePickerDemo’ or as you wish.





Step 3-Now go into the ‘Main.storyboard’ and create platform bellow type. Here ‘Pick From Gallery’ and ‘Pick From Camera’ both are buttons.

Step 4- Now go into ‘ViewController.h’ and give delegate.
UIImagePickerControllerDelegateandUINavigationControllerDelegate’.

Step 5-Now give ‘IBOutlet’ of image view and buttons and also give ‘Action’ of buttons and write bellow type code.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController *imagePickerCtrl;
    UIPopoverController *popOver;
}
@property (strong, nonatomic) IBOutlet UIImageView *pickedImageView;
@property (strong, nonatomic) IBOutlet UIButton *btnGallery;
@property (strong, nonatomic) IBOutlet UIButton *btnCamera;

- (IBAction)btnGalleryAction:(id)sender;
- (IBAction)btnCameraAction:(id)sender;

@end


Step 6-Now go into ‘ViewController.m’ and write into ‘Pick from Gallery’ button button.

- (IBAction)btnGalleryAction:(id)sender {
    imagePickerCtrl= [[UIImagePickerController alloc] init];
    imagePickerCtrl.delegate = self;
    imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
        [self presentViewController:imagePickerCtrl animated:YES completion:nil];
    else
    {
        popOver=[[UIPopoverController alloc]initWithContentViewController:imagePickerCtrl];
        [popOver presentPopoverFromRect:_btnGallery.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }    
}


Step 7-Now write into the ‘Pick from Camera’ button.

- (IBAction)btnCameraAction:(id)sender {
    imagePickerCtrl = [[UIImagePickerController alloc] init];
    imagePickerCtrl.delegate = self;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePickerCtrl animated:YES completion:NULL];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"No Camera Available." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
    }
}

Step 8-After that write ‘imagePickerController’ delegate method.

#pragma mark - ImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) {
        [picker dismissViewControllerAnimated:YES completion:nil];
    } else {
        [popOver dismissPopoverAnimated:YES];
    }
    _pickedImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

Step 9-Now run your project and you can see bellow type window.

Step 10-When you press ‘Pick from Gallery’ button then you can see your gallery and pick one .

Step 11-Now if you working on your pc that time press ‘Pick from Camera’ so you can see bellow type alert otherwise if you use iPhone or iPad then properly work in this. 


For more relaxation, Click Here. 

Comments

Popular posts from this blog

Dera Sacha Sauda chief Gurmeet Ram Rahim Singh - News Upword