{"id":65558,"date":"2024-10-20T10:02:57","date_gmt":"2024-10-20T04:32:57","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=65558"},"modified":"2024-10-24T15:36:24","modified_gmt":"2024-10-24T10:06:24","slug":"custom-camera-functionality-with-avfoundation-and-vision-kit-part-1-3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-camera-functionality-with-avfoundation-and-vision-kit-part-1-3\/","title":{"rendered":"Enhanced Camera with AVFoundation"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>AVFoundation is a powerful framework provided by Apple for working with audiovisual media. It offers robust support for capturing, processing, and editing audio and video content. In this blog, we&#8217;ll explore how to leverage AVFoundation to build advanced camera functionalities for iOS applications.<\/p>\n<h3>Getting Started with AVFoundation<\/h3>\n<h4>What is AVFoundation?<\/h4>\n<p>AVFoundation is a framework that provides a set of APIs for working with time-based audiovisual media. It supports various tasks such as capturing, processing, and editing audio and video.<\/p>\n<h4>Setting Up Your Project<\/h4>\n<p>To use AVFoundation, you need to:<\/p>\n<ol>\n<li><strong>Add AVFoundation Framework:<\/strong> Go to your Xcode project settings, select your target, and add AVFoundation.framework under the &#8220;Frameworks, Libraries, and Embedded Content&#8221; section.<\/li>\n<li><strong>Request Permissions:<\/strong> Ensure that you request and handle permissions for camera access in your app\u2019s Info.plist. You need to add:<\/li>\n<\/ol>\n<ul>\n<li style=\"text-align: left;\">NSCameraUsageDescription (Privacy &#8211; Camera Usage Description)<\/li>\n<\/ul>\n<h3>Capturing Video with AVFoundation<\/h3>\n<h4>Basic Camera Setup<\/h4>\n<p>To capture video, you need to set up a few core components:<\/p>\n<ul>\n<li><strong>AVCaptureSession:<\/strong> Manages the flow of data from the input devices to the output.<\/li>\n<\/ul>\n<div id=\"attachment_68269\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68269\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68269 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession-1024x101.png\" alt=\".\" width=\"625\" height=\"62\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession-1024x101.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession-300x30.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession-768x76.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession-624x62.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureSession.png 1074w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68269\" class=\"wp-caption-text\">To initialize the session<\/p><\/div>\n<ul>\n<li><strong>AVCaptureDevice:<\/strong> Represents the physical camera on the device.<\/li>\n<\/ul>\n<div id=\"attachment_68267\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68267\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68267 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice-1024x461.png\" alt=\".\" width=\"625\" height=\"281\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice-1024x461.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice-300x135.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice-768x346.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice-624x281.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDevice.png 1298w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68267\" class=\"wp-caption-text\">To get the camera from lenses<\/p><\/div>\n<ul>\n<li><strong>AVCaptureDeviceInput:<\/strong> Connects the AVCaptureDevice to the session.<\/li>\n<\/ul>\n<div id=\"attachment_68268\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68268\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68268 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput-1024x520.png\" alt=\".\" width=\"625\" height=\"317\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput-1024x520.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput-300x152.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput-768x390.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput-624x317.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureDeviceInput.png 1224w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68268\" class=\"wp-caption-text\">To set the inputs for the camera<\/p><\/div>\n<ul>\n<li><strong>AVCaptureVideoDataOutput:<\/strong> Handles the video data output.<\/li>\n<\/ul>\n<div id=\"attachment_68270\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68270\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68270 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1024x383.png\" alt=\".\" width=\"625\" height=\"234\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1024x383.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-300x112.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-768x287.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1536x574.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-624x233.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput.png 1900w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68270\" class=\"wp-caption-text\">To get video output from camera<\/p><\/div>\n<ul>\n<li><strong>AVCapturePhotoOutput:<\/strong> Handles the photo data output.<\/li>\n<\/ul>\n<div id=\"attachment_68270\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68270\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68270 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1024x383.png\" alt=\".\" width=\"625\" height=\"234\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1024x383.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-300x112.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-768x287.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-1536x574.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput-624x233.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/AVCaptureVideoDataOutput.png 1900w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68270\" class=\"wp-caption-text\">To get video output from the camera<\/p><\/div>\n<ul>\n<li><strong>Preview Layer:<\/strong> To display the camera feed on the screen.\n<p><div id=\"attachment_68275\" style=\"width: 635px\" class=\"wp-caption alignleft\"><img aria-describedby=\"caption-attachment-68275\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68275 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer-1024x79.png\" alt=\".\" width=\"625\" height=\"48\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer-1024x79.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer-300x23.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer-768x59.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer-624x48.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Preview-Layer.png 1374w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68275\" class=\"wp-caption-text\">To set the camera layer<\/p><\/div><\/li>\n<\/ul>\n<h2><\/h2>\n<h3>Starting and Stopping the Session<\/h3>\n<ul>\n<li>Start the session when you\u2019re ready to begin capturing:<\/li>\n<\/ul>\n<div id=\"attachment_68276\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68276\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68276 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting-1024x189.png\" alt=\".\" width=\"625\" height=\"115\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting-1024x189.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting-300x55.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting-768x142.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting-624x115.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Starting.png 1528w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68276\" class=\"wp-caption-text\">To start the camera session<\/p><\/div>\n<ul>\n<li>And stop it when you need to:<\/li>\n<\/ul>\n<div id=\"attachment_68277\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68277\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68277 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping-1024x247.png\" alt=\".\" width=\"625\" height=\"151\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping-1024x247.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping-300x72.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping-768x185.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping-624x151.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Stopping.png 1044w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68277\" class=\"wp-caption-text\">To stop the camera session<\/p><\/div>\n<h2>Advanced Camera Features<\/h2>\n<ul>\n<li>Focus\n<ul>\n<li>Constants indicate the mode of the focus on the receiver&#8217;s device. To set the focus of the camera you can use the device <span style=\"text-decoration: underline;\">focusMode<\/span> property.<\/li>\n<li>There are three types of focus mode.\n<ol>\n<li><strong>locked<\/strong>: Indicates that the focus should be locked at the lens&#8217; current position.<\/li>\n<li><strong>autoFocus<\/strong>: Indicates that the device should autofocus once and then change the focus mode to AVCaptureFocusModeLocked.<\/li>\n<li><strong>continuousAutoFocus<\/strong>: Indicates that the device should automatically focus when needed.<\/li>\n<\/ol>\n<\/li>\n<li>To set the focus mode you first need to check whether the mode is supported or not on that device by using<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div id=\"attachment_68273\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68273\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68273 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-1024x168.png\" alt=\".\" width=\"625\" height=\"103\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-1024x168.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-300x49.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-768x126.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-624x103.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus.png 1034w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68273\" class=\"wp-caption-text\">To set the focus of the camera<\/p><\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>To set device configuration you should be in device configuration mode enabled to change the device camera setting. This can be achieved by using<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div id=\"attachment_68274\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68274\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68274 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1-1024x357.png\" alt=\".\" width=\"625\" height=\"218\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1-1024x357.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1-300x105.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1-768x268.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1-624x218.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Focus-2-1.png 1136w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68274\" class=\"wp-caption-text\">Error handling while setting the focus of the camera<\/p><\/div>\n<ul>\n<li>Exposure\n<ul>\n<li>Constants indicate the mode of the exposure on the receiver&#8217;s device if it has adjustable exposure. To set the focus of the camera you can use the device <span style=\"text-decoration: underline;\">exposureMode<\/span> property.<\/li>\n<li>In simple words, exposure means the amount of light that reaches a camera&#8217;s sensor or film when a picture is taken.<\/li>\n<li>It helps to capture slow-movement and fast-movement objects.<\/li>\n<li>To control exposure you can set the exposureMode property\n<ol>\n<li><strong>locked<\/strong>: Indicates that the exposure should be locked at its current value.<\/li>\n<li><strong>autoExpose<\/strong>: Indicates that the device should automatically adjust exposure once and then change the exposure mode to AVCaptureExposureModeLocked.<\/li>\n<li><strong>continuousAutoExposure<\/strong>: Indicates that the device should automatically adjust exposure when needed.<\/li>\n<li><strong>custom<\/strong>: Indicates that the device should only adjust exposure according to user-provided ISO and exposure Duration values.\n<ul>\n<li>When you set exposure mode custom you should call the function <strong>setExposureModeCustom.<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div id=\"attachment_68272\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68272\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68272 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure-1024x298.png\" alt=\".\" width=\"625\" height=\"182\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure-1024x298.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure-300x87.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure-768x223.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure-624x182.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Exposure.png 1272w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68272\" class=\"wp-caption-text\">Handle the Exposure setting of the camera<\/p><\/div>\n<div id=\"attachment_68271\" style=\"width: 635px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-68271\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-68271 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure-1024x346.png\" alt=\".\" width=\"625\" height=\"211\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure-1024x346.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure-300x101.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure-768x260.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure-624x211.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/10\/Custom-Exposure.png 1314w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-68271\" class=\"wp-caption-text\">To set the custom exposure of the camera<\/p><\/div>\n<p>Here is the sample code repository you can check out <a href=\"https:\/\/github.com\/raja-2024\/EnhancedCamera\">here<\/a>.<\/p>\n<h3><span style=\"font-size: 1.28571rem;\">Conclusion<\/span><\/h3>\n<p>AVFoundation is an incredibly versatile framework that allows you to harness the full potential of the camera hardware on iOS devices. With proper setup and configuration, you can create sophisticated camera functionalities for your apps. Keep experimenting with AVFoundation\u2019s features to fully explore what\u2019s possible!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction AVFoundation is a powerful framework provided by Apple for working with audiovisual media. It offers robust support for capturing, processing, and editing audio and video content. In this blog, we&#8217;ll explore how to leverage AVFoundation to build advanced camera functionalities for iOS applications. Getting Started with AVFoundation What is AVFoundation? AVFoundation is a framework [&hellip;]<\/p>\n","protected":false},"author":1952,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":266},"categories":[1400],"tags":[6459,6458],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65558"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1952"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=65558"}],"version-history":[{"count":13,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65558\/revisions"}],"predecessor-version":[{"id":68479,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/65558\/revisions\/68479"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=65558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=65558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=65558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}