Swift Camera & Gallery

Hello friends, in this article, we will talk about how we can make photo and video operations with Swift. In almost every application, we ask for media from the user. This is why this issue is important here.

This example will be an image at the top and a button below. When the user presses the button, we will request a photo or picture from the camera or gallery. We need to ask the user for permission to access the phone’s camera or gallery. For this, we have to write the necessary permissions from the info.plist file. I entered here as an example. It is necessary to pay attention to these while throwing the application to the stores. You need to explain exactly what you’re going to use it for or Apple may refuse.

Sample screen design for this application is as follows.

Then we can start writing code. First we need to create a UIImagePicker. With this image picker, we will be able to access the phone’s camera or gallery. If you only work with images, you can add image “public.image” in UIImagePicker media types, but if you also allow the user to add videos, you need to add “public.movie”. Then, when you click the button, we bring up an Action Sheet. Here we are asking whether the user will choose the media from the camera or the gallery. Accordingly, we change the source type of UIImagePicker.

 

After the user selects one of the camera or gallery, he falls into the didFinishPickingMediaWithInfo function of the UIImgaePicker. The ViewController we use must inherit from the UIImagePickerControllerDelegate, UINavigationControllerDelegate interfaces to fall into this. After falling into the function, you can set here according to the type of media. Another process is that you can take the user’s status after editing the photo or video.

 

If the user has selected an image, we can show it directly with a UIImageView, but if the user has chosen you can show it with AVPlayer or a pod. You can find a detailed article about AVPlayer in the following articles.

The screen output is as follows.

If you have questions, you can reach by e-mail or comment. Good work.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.