Picking Image from Gallery and Camera iPhone Swift

Most of the time we require Picking Image from Gallery and Camera iPhone Swift.  iOS provides the UIImagePickerController which is helpful in picking images from gallery or to take photos using the built-in camera of an iOS device.

In our last tutorial we learn how to use here..  In todays tutorial we will display image in UIImageView and use UIButton to initiate camera or to show gallery.

 

Create New Project : ImaGer

Open Xcode start a new project > name it to Imager. Select Language Swift. I’m using Swift 2.1 and Xcode 7.2.1.

 

Picking Image from Gallery and Camera iPhone Swift
Picking Image from Gallery and Camera iPhone Swift

 

Working on UI of Project.

We will user 2 buttons one to pick image from gallery other to open camera and click pic. And one image view to display image clicked/picked. I have used constraints in this project.

Picking Image from Gallery and Camera iPhone Swift

Creating Outlets on controller.

Press Control and drag the object on ViewController to create its outlet. Do it for all 3 objects.We will create button actions on View

Picking Image from Gallery and Camera iPhone Swift

 

Picking Image From Gallery.

Add  UINavigationControllerDelegate, UIImagePickerControllerDelegate to your ViewController. UIimagePickerController  is the user interface for managing the user interaction with the camera or with the gallery.

Create an object for your UIImagePickerController.

 

Initiating imagePicker

 

 

This will open your gallery on simulator or phone like below.

Picking Image from Gallery and Camera iPhone Swift

Try selecting any image. It will disappear but how to get that selected image. For doing that we must add UIImagePickerControllerDelegate methods.

 

 

Adding delegate methods.

The first method is called when you select any image from gallery and then it will assign that image to our image controller.

2nd method is called when you click on the cancel button it will dismiss the UIImagePickerController.

Now try again. It will look like below.

Picking Image from Gallery and Camera iPhone Swift

 

 

Well Done. Great job (Y)

 

Click image from Built-in-camera.

From our Picking Image from Gallery and Camera iPhone Swift the 2nd part is left. First we will initiate the camera. Its same as of gallery some how.

The first is to check if camera is available or not if you don’t put that check you will end up crashing your app in simulator.

 

 

Try clicking button using simulator. It will look like this .

 

 

Picking Image from Gallery and Camera iPhone Swift

 

 

See “Camera not available” as output we need to run our code in real device.

 

 

Picking Image from Gallery and Camera iPhone Swift

 

Above are 3 screenshots from left when you click on”Open Camera” button 2nd is when you clicked and took picture 3rd is picture displayed after clicking on “Use Photo”.

The delegate methods we used for picking image from gallery are used here too are both are of UIImageController.

 

You can download whole project from here.

So today we learn Picking Image from Gallery and Camera iPhone Swift. Will be back soon with something new this time. Till then Happy Coding 😉

 

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.