{"id":1694,"date":"2021-02-28T19:36:30","date_gmt":"2021-02-28T19:36:30","guid":{"rendered":"https:\/\/sezeromer.com\/?p=1694"},"modified":"2023-03-04T13:30:04","modified_gmt":"2023-03-04T10:30:04","slug":"swift-camera-gallery","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/swift-camera-gallery\/","title":{"rendered":"Swift Camera &#038; Gallery"},"content":{"rendered":"<p>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.<\/p>\n<p>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&#8217;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&#8217;re going to use it for or Apple may refuse.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2012\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.18.08.png\" alt=\"\" width=\"657\" height=\"66\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.18.08.png 657w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.18.08-300x30.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.18.08-500x50.png 500w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/p>\n<p>Sample screen design for this application is as follows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2013\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-1024x576.png\" alt=\"\" width=\"640\" height=\"360\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-1024x576.png 1024w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-300x169.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-768x432.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-500x281.png 500w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-800x450.png 800w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-1280x720.png 1280w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52-1536x864.png 1536w, https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/Screen-Shot-2021-02-28-at-22.21.52.png 1920w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Then we can start writing code. First we need to create a <strong>UIImagePicker<\/strong>. With this image picker, we will be able to access the phone&#8217;s camera or gallery. If you only work with images, you can add image &#8220;public.image&#8221; in <strong>UIImagePicker<\/strong> media types, but if you also allow the user to add videos, you need to add &#8220;public.movie&#8221;. 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.<\/p>\n<p>&nbsp;<\/p>\n<p>After the user selects one of the camera or gallery, he falls into the didFinishPickingMediaWithInfo function of the <strong>UIImgaePicker<\/strong>. The ViewController we use must inherit from the <strong>UIImagePickerControllerDelegate<\/strong>, <strong>UINavigationControllerDelegate<\/strong> 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&#8217;s status after editing the photo or video.<\/p>\n<p>&nbsp;<\/p>\n<p>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 <strong>AVPlayer<\/strong> in the following articles.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/\r\n\/\/  ViewController.swift\r\n\/\/  SwiftCameraGallery\r\n\/\/\r\n\/\/  Created by Omer Sezer on 28.02.2021.\r\n\/\/\r\n\r\nimport UIKit\r\n\r\nclass ViewController: UIViewController {\r\n\r\n    @IBOutlet weak var ivImage: UIImageView!\r\n    @IBOutlet weak var btnMedia: UIButton!\r\n    \r\n    let imagePicker = UIImagePickerController()\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \r\n        imagePicker.delegate = self\r\n        imagePicker.sourceType = .photoLibrary\r\n        imagePicker.mediaTypes = [\"public.image\", \"public.movie\"]\r\n        \r\n        btnMedia.addTarget(self, action: #selector(onBtnMediaClicked(_:)), for: .touchUpInside)\r\n    }\r\n    \r\n    @objc func onBtnMediaClicked(_ sender: UIButton) {\r\n        let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)\r\n        \r\n        let btnCamera = UIAlertAction(title: \"Kamera\", style: .default) { (action) in\r\n            self.imagePicker.sourceType = .camera\r\n            self.present(self.imagePicker, animated: true)\r\n        }\r\n        \r\n        let btnGallery = UIAlertAction(title: \"Galeri\", style: .default) { (action) in\r\n            self.imagePicker.sourceType = .photoLibrary\r\n            self.present(self.imagePicker, animated: true)\r\n        }\r\n        \r\n        let btnCancel = UIAlertAction(title: \"Vazge\u00e7\", style: .destructive) { (action) in\r\n            \r\n        }\r\n        optionMenu.addAction(btnCamera)\r\n        optionMenu.addAction(btnGallery)\r\n        optionMenu.addAction(btnCancel)\r\n        self.present(optionMenu, animated: true)\r\n    }\r\n\r\n}\r\n\r\nextension ViewController: UIImagePickerControllerDelegate {\r\n    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {\r\n        let mediaType = info[.mediaType] as? String\r\n        switch mediaType {\r\n        case \"public.movie\":\r\n            if let videoUrl = info[.mediaURL] as? NSURL {\r\n                \/\/ TODO : set video to avplayer\r\n                print(videoUrl)\r\n            }\r\n        case \"public.image\":\r\n            if let image = info[.originalImage] as? UIImage {\r\n                ivImage.image = image\r\n            }\r\n        default:\r\n            break\r\n        }\r\n        self.dismiss(animated: true)\r\n    }\r\n}\r\n\r\nextension ViewController: UINavigationControllerDelegate {\r\n    \r\n}<\/pre>\n<\/div>\n<p>The screen output is as follows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2014\" src=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2021\/02\/1-1.gif\" alt=\"\" width=\"401\" height=\"857\" \/><\/p>\n<p>If you have questions, you can reach by e-mail or comment. Good work.<\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[327],"tags":[775,509,152,777,115,148,189,328,776,162],"class_list":["post-1694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-avplayer","tag-delegate","tag-image","tag-image-picker","tag-ios","tag-picker","tag-resim","tag-swift","tag-uiimagepicker","tag-video"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1694"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1694"}],"version-history":[{"count":5,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1694\/revisions"}],"predecessor-version":[{"id":2665,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1694\/revisions\/2665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/2014"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}