×

iOS UIActionSheet & UIImagePickerController


How to choose an image from gallery or click from camera ?

UIImagePickerController is used to choose an image from gallery or allows you to take an image from camera. UIIMagePickerController provides sourceType property which helps app to decide wether image to choose from gallery or from camera.

UIImagePickerController Properties

1) .editing

2) .sourceType = UIImagePickerControllerSourceTypeCamera | UIImagePickerControllerSourceTypePhotoLibrary

UIImagePickerController Delegate Method

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

This delegate method is used to pick an media from specified sourceType . SourceType may be of two types UIImagePickerControllerSourceTypeCamera and UIImagePickerControllerSourceTypePhotoLibrary

UIActionSheet

UIActionSheet provides nice way to choose from various Buttons you specified. in our case we add buttons named as Gallery & Camera

Final Output


The Gallery & Camera Button you see in Final output image is given by UIActionSheet. This is the way you can alloc an UIActionShhet.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Gallery",@"Camera",nil];

[actionSheet showInView:self.view];
 

UIActionShhet Delegate Methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

This delegate method is used to choose which button on ActionSheet is clicked with the help of buttonIndex specified in delegate Method.

Complete code for final Output

// Created by Manish Methani 
// Copyright © 2017 Codzify. All rights reserved.
//
#import "ViewController.h"

@interface ViewController () 
{
    UIButton* imageButton;
}
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
  /* All about Navigation Controller Title , Back Button */
    NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    
  /* give Background color to navigation bar */
    if ([[ver objectAtIndex:0] intValue] >= 7) {
        // iOS 7.0 or later
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
        self.navigationController.navigationBar.translucent = NO;
    }else {
        // iOS 6.1 or earlier
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
    }
    
  /* give title to navigation bar */
    self.navigationItem.title = @"First View Controller";
    
  /* give titleColor to Navigation Title */
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    
  

/* Initialize ImageUploadView */

[self initializeImageUploadView]; } - (void)initializeImageUploadView { imageButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 50, 120, 100, 100)]; imageButton.layer.masksToBounds = YES; imageButton.layer.cornerRadius = 50; imageButton.layer.borderWidth = 2.0; [imageButton setTitle:@"Add Photo" forState:UIControlStateNormal]; imageButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0]; imageButton.titleLabel.numberOfLines = 2; imageButton.titleLabel.textAlignment = NSTextAlignmentCenter; [imageButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; imageButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); [imageButton addTarget:self action:@selector(changeImage) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:imageButton]; UILabel *labelExample = [[UILabel alloc]initWithFrame:CGRectMake(12, CGRectGetMaxY(imageButton.frame), 360, 80)]; [self.view addSubview:labelExample]; labelExample.text = @"Example of how to choose an image from gallery or camera."; labelExample.textColor = [UIColor colorWithRed:0/255.0 green:169/255.0 blue:98/255.0 alpha:1.0]; labelExample.font = [UIFont fontWithName:@"Helvetica-bold" size:20.0]; labelExample.numberOfLines = 0; labelExample.lineBreakMode = NSLineBreakByWordWrapping; } - (void)changeImage { [self.view endEditing:YES]; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Gallery",@"Camera",nil]; [actionSheet showInView:self.view]; } #pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: { [self galleryBtnPressed]; } break; case 1: { [self cameraBtnPressed]; } break; default: break; } } - (void)galleryBtnPressed { UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:nil]; } - (void)cameraBtnPressed { UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; imagePickerController.delegate = self; [self presentViewController:imagePickerController animated:YES completion:nil]; } #pragma mark - UIIMagePickerController Delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; [imageButton setImage:chosenImage forState:UIControlStateNormal]; [picker dismissViewControllerAnimated:YES completion:NULL]; } @end

Make sure to include these Delegates after interface,

@interface ViewController () <UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>