Introduction
In this article, we will learn how to access the camera and Photo Library in iOS app development. This is the key feature and a very important one because the clients often require these features in their app. If you are familiar with Android app development, then you can easily access the camera because it is similar in both systems. But, if you are not familiar with Android, follow my article and concentrate on each step. It’s not too hard. So, let’s get started.
Step 1
Open the Xcode (IDE) and create a new project. Give it the name as “CameraApp” and press Next. Now, choose the Single View Application and set the application type to universal. Then, finish creating the project. After that, drag two buttons and one image View from the object library and drop it into the Main Storyboard file. Make the interface like the following.
Step 2
Now, at the top position in the right side, you see the Assistant editor option. Click on this. Now, your coding screen break into two parts. Next, we make the outlet of two buttons and one image View, like the following:
Step 3
Now, you make action of two buttons, known as Photo Library and Camera. Remember that the action is created after the didRecieveMemory function. Also, remember that you need to select the connection type action not outlet.
Step 4
Now, you have two outlets. One is Photo Library and another one is camera. At the top, you can see the ViewController class inherits the UIViewController class. Now, you can declare two more classes after UIViewController. I also have given the code of two buttons. Paste this code into outlet action and your project is ready.
Code
-
-
-
-
-
-
-
- import UIKit
- class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
- {
- @IBOutlet weak
- var PhotoLibaray: UIButton!@IBOutlet weak
- var ImageDisplay: UIImageView!@IBOutlet weak
- var cameraa: UIButton!override func viewDidLoad() {
- super.viewDidLoad()
-
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
-
- }
- @IBAction func PhotoLibraryAction(sender: UIButton) {
- let picker = UIImagePickerController()
- picker.delegate = self
- picker.sourceType = .PhotoLibrary
- presentViewController(picker, animated: true, completion: nil)
- }
- @IBAction func CameraAction(sender: UIButton) {
- let picker = UIImagePickerController()
- picker.delegate = self
- picker.sourceType = .Camera
- presentViewController(picker, animated: true, completion: nil)
- }
- func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {
- ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as ? UIImage;
- dismissViewControllerAnimated(true, completion: nil)
- }
- }
Last Step
Now, run the app. You see that when you click the Photo library, it gives you the option Allow. Now, the mobile library is open. You can't see anything but when you click on camera, the app fails because we have no real device for checking.