Share

tweet



Developing an iOS application that can implement the integral feature of the targeted device can add superior functionality to your application.

An application success depends upon how creatively you have designed your application and what all features does your app offers.

Whether you are interested in creating a messenger application or any other application that allows users to use the device’s inbuilt camera function and some integral features, this post will help you achieve your goal efficiently. For managing the interactions with the photo library or Camera, the iOS library includes the UIImagePickerController class. And, for responding to the interactions, this class will need to implement a delegate.

Let’s ponder into the step-by-step guide and begin with the iOS application development process.

1. How to create a new project

Note: Here Xcode platform has been deployed.

Navigate to the Xcode and choose the “Single View Application” template while creating a new project. And click on the Next button.

By doing so, you will get the next page on the screen, there simply fill the specified details including product name and company identifier. Choose the targeted device from the drop-down menu and only check the button corresponding to the statement “Use Antomatic Reference Counting”. Click on Next button.

2. How to Design the App UI

To showcase the View Controller on the Editor Pane , simply access the Xcode’s Navigator and select the AppViewController.xib file. Now, from the library select an UIImageView and drag it and drop it at the top in the UIView.

on the , simply access the Xcode’s Navigator and select the file. Now, from the library select an UIImageView and drag it and drop it at the top in the UIView. Set the desirable dimensions of the image screen.

Now, drag and drop the two UIButtons at the bottom of the screen (that is, below the UIImageView), and name them as “Click Photo” and “Select Photo” .

and . To restrict the set size, you need to make the following tweaks:

– Access the UIImageView.

– Under the File tab in the Inspector, keep the statement “Use AutoLayout Option” as unchecked.

After following these steps, your application interface will be ready. However, you will need to define the connections. Here are the steps that can help you create connections within the UI elements.

Switch the Editor to the Assistant mode.

to the mode. While selecting the AppViewController.xib file, open the AppViewController.h file.

file, open the file. Press Control and drag mouse from the UIImageView to the AppViewController.h file.

As shown in the above image, fill the details in the fields as follow:

Set the Connection field as Outlet

field as Outlet Name = imageView

= imageView Define Type as UIImageView

as UIImageView Storage = Strong

With this, you get the IBOutlet code as –

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

Similarly, for the “Click Photo” button, make tweaks as mentioned below.

Set the Connection field as Action

field as Action Name = clickPhoto

= clickPhoto Define Type as UIButton

as UIButton Set the Event that will initiate the corresponding action as “Touch Up Inside”.

that will initiate the corresponding action as “Touch Up Inside”. The Method will include sender as an argument.

Likewise, fill the fields for the “select Photo” button, however, don’t forget to set the Name as selectPhoto.

After completing all the aforementioned steps successfully, the APPviewController.h will hold the following piece of code.

Code Snippet:

#import @interface APPViewController : UIViewController @property (strong, nonatomic) IBOutlet UIImageView *imageView; – (IBAction)takePhoto: (UIButton *)sender; – (IBAction)selectPhoto:(UIButton *)sender; @end

3. How to Implement the View Controller

In order to ensure user interaction with the built-in photo library and device’s camera via the app, you will need to conform to the UIImagePickerControllerDelegate protocol and UINavigationControllerDelegate protocol.

Simply add both the protocols to the AppViewController.h file with –

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

e For the “Click Photo” button:

Create an UIImagePickerController .

. Set the delegates to the AppViewController class.

class. Assign the UIImagePickerControllerSourceTypeCamera as the image picker for the camera and UIImagePickerControllerSourceTypePhotoLibrary as the image picker for photo library.

as the image picker for the camera and as the image picker for photo library. Finally, to represent the picker modally, implement the presentViewController method.

Code Snippet:

– (IBAction)clickPhoto:(UIButton *)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:NULL]; }

e For the “Select Photo” button: you can follow the same steps as mentioned above and set the sourceType as UIImagePickerControllerSourceTypePhotoLibrary.

Code Snippet:

– (IBAction)selectPhoto:(UIButton *)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL]; }

4. How to Implement The Delegate Methods of UIImagePickerController

Whenever a user tries to resize the clicked photograph, the didFinishPickingMediaWithInfo is executed. This method holds two arguments –

The first argument is the picker, who has called the method.

The second argument is a NSDictionary, it features the both the original and the edited image. The edited image can also be accessed via UIImagePickerControllerEditedImage tag.

Code Snippet:

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

5. Test your Application

By following all the aforementioned steps thoroughly your application will be completed. Now, to ensure that your app will run smoothly, you will need to test it before launching it. An iPhone emulator won’t help you accomplish the task as it doesn’t features a camera, however, you may use an iOS device (like iPhone, iPad, etc.). Simply load the app and check whether it is running smoothly or not.

This step-by-step guide will help you create an iOS application that facilitates users to use the device camera and access the built-in photo library.