{"id":1264,"date":"2014-09-20T18:20:51","date_gmt":"2014-09-21T00:20:51","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=1264"},"modified":"2014-09-20T18:31:22","modified_gmt":"2014-09-21T00:31:22","slug":"taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/","title":{"rendered":"Taking control of the iPhone camera in iOS 8 with Swift (Part 2)"},"content":{"rendered":"<p>Using the AVFoundation API, we are going to set up a capture session and make an app that allows us to use all the new fine-grained controls added to iOS 8. This includes manually controlling focus, exposure, and ISO. First off, we just need to set up a basic camera preview. In Part 1, we created a touch-based way to manually control focus. If you haven&#8217;t read that yet you can <a href='http:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1\/'>read it here<\/a>.<\/p>\n<p>In this part of the tutorial, we&#8217;re going to dive in to the API a little deeper and create a second axis for controlling our image using the vertical touch position.<\/p>\n<p>First off, let&#8217;s just clean up this code that determines how far in to the screen is being touched. Let&#8217;s create a function that takes in a UITouch object, and returns a CGPoint specifying how far in to the screen we&#8217;ve tapped, as a percentage of the screens total width. For example tapping on the very top-right of the screen will return a CGPoint with values 1,1. While tapping on the bottom-left will return 0,0&#8230; the center of the screen 0.5, 0.5, and all values in between.<\/p>\n<pre class=\"brush: js;\">\r\nfunc touchPercent(touch : UITouch) -> CGPoint {\r\n    \/\/ Get the dimensions of the screen in points\r\n    let screenSize = UIScreen.mainScreen().bounds.size\r\n    \r\n    \/\/ Create an empty CGPoint object set to 0, 0\r\n    var touchPer = CGPointZero\r\n    \r\n    \/\/ Set the x and y values to be the value of the tapped position, divided by the width\/height of the screen\r\n    touchPer.x = touch.locationInView(self.view).x \/ screenSize.width\r\n    touchPer.y = touch.locationInView(self.view).y \/ screenSize.height\r\n    \r\n    \/\/ Return the populated CGPoint\r\n    return touchPer\r\n}\r\n<\/pre>\n<p>This declares a method named <strong>touchPercent<\/strong>, which takes a <strong>UITouch<\/strong> object named <strong>touch<\/strong> as an argument, and returns a <strong>CGPoint<\/strong>. The actual math is simple&#8230; take the touched point and divide by the total number of points for either the width or height of the screen. We are already using the x value for focus, but we need to adjust the <strong>touchesBegan<\/strong> and <strong>touchesMoved<\/strong> method to use our new function.<\/p>\n<pre class=\"brush: js;\">\r\noverride func touchesBegan(touches: NSSet, withEvent event: UIEvent) {\r\n    let touchPer = touchPercent( touches.anyObject() as UITouch )\r\n    focusTo(Float(touchPer.x))\r\n}\r\n\r\noverride func touchesMoved(touches: NSSet, withEvent event: UIEvent) {\r\n    let touchPer = touchPercent( touches.anyObject() as UITouch )\r\n    focusTo(Float(touchPer.x))\r\n}\r\n<\/pre>\n<p><i>Note: you can remove the old <strong>screenWidth<\/strong> variable we were declaring before.<\/i><\/p>\n<p>Next, we want to specify the ISO. If you&#8217;re unfamiliar with that is, it&#8217;s simple enough to say that the ISO is a setting for digital cameras that specifies what amount of light to allow in to the lens. A lower ISO will give brighter and higher quality pictures, but with a slower shutter speed, resulting in blurrier photos. A very low ISO results in a camera mode where a tripod is useful. With a very high ISO, you can take quick action shots with the very low shutter speed, but expect to see lower quality images with more noise. I&#8217;m by no means a professional photographer, so I&#8217;m probably not explaining it all that well, but that&#8217;s about my level of understanding.<\/p>\n<p>Okay, moving on&#8230; let&#8217;s just add it to the app and see what it does for ourselves \ud83d\ude42<\/p>\n<p>First, let&#8217;s update the focusTo() method to take in an isoValue in addition to a focusValue<\/p>\n<pre class=\"brush: js;\">\r\nfunc updateDeviceSettings(focusValue : Float, isoValue : Float) {\r\n    if let device = captureDevice {\r\n        if(device.lockForConfiguration(nil)) {\r\n            device.setFocusModeLockedWithLensPosition(focusValue, completionHandler: { (time) -> Void in\r\n                \/\/\r\n            })\r\n            \r\n            \/\/ Adjust the iso to clamp between minIso and maxIso based on the active format\r\n            let minISO = device.activeFormat.minISO\r\n            let maxISO = device.activeFormat.maxISO\r\n            let clampedISO = isoValue * (maxISO - minISO) + minISO\r\n            \r\n            device.setExposureModeCustomWithDuration(AVCaptureExposureDurationCurrent, ISO: clampedISO, completionHandler: { (time) -> Void in\r\n                \/\/\r\n            })\r\n            \r\n            device.unlockForConfiguration()\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>The first new thing in this function is the name is now <strong>updateDeviceSettings<\/strong>. This is just because we&#8217;re adding more than focus. We&#8217;ve also specified <strong>focusValue<\/strong> and <strong>isoValue<\/strong> as distinct values.<\/p>\n<p>Next, we have the normal locking and setting of the focus, followed by an additional bit of code that stored the minISO and maxISO, then adjusts our isoValue to fit within those two values, proportionally.<\/p>\n<p>For example, if <strong>minISO<\/strong> is 50, and <strong>maxISO<\/strong> is 100. A value of 0 passed on to <strong>isoValue<\/strong> will result in a <strong>clampedISO<\/strong> of 50. But a value of 0.5 will result in a <strong>clampedISO<\/strong> of 75.<\/p>\n<p>After adjusting this value, we can call the method that actually sets the ISO, <strong>setExposureModeCustomWithDuration()<\/strong>.<\/p>\n<p>The first parameter to this method is the shutter speed, in this case we aren&#8217;t trying to specify shutter speed so we simply use the constant <strong>AVCaptureExposureDurationCurrent<\/strong>. This basically just says we don&#8217;t want to specify a shutter time, and are just modifying the ISO. Second, is our <strong>clampedISO<\/strong> value, then finally we have a completionHandler just as we do with the <strong>setFocusModeLockedWithLensPosition<\/strong> method.<\/p>\n<p>Try running the app and sliding your finger from the top to the bottom of the screen. Do you see the difference in the general pixel brightness? This is the ISO being modified in real time.<\/p>\n<p>In the next part, we&#8217;ll dig a little deeper and naturally, implement some more UI to control these settings.<\/p>\n<p>Here is the final code from this post:<br \/>\n<a href='https:\/\/github.com\/jquave\/CameraTutorial\/tree\/Part2'>Part 2 on Github<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the AVFoundation API, we are going to set up a capture session and make an app that allows us to use all the new fine-grained controls added to iOS 8. This includes manually controlling focus, exposure, and ISO. First off, we just need to set up a basic camera preview. In Part 1, we&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[25,10,32],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"Using the AVFoundation API, we are going to set up a capture session and make an app that allows us to use all the new fine-grained controls added to iOS 8. This includes manually controlling focus, exposure, and ISO. First off, we just need to set up a basic camera preview. In Part 1, we...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-21T00:20:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-09-21T00:31:22+00:00\" \/>\n<meta name=\"author\" content=\"Jameson Quave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jameson Quave\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/\",\"name\":\"Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2014-09-21T00:20:51+00:00\",\"dateModified\":\"2014-09-21T00:31:22+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Taking control of the iPhone camera in iOS 8 with Swift (Part 2)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\",\"name\":\"Jameson Quave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"caption\":\"Jameson Quave\"},\"sameAs\":[\"http:\/\/jamesonquave.com\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/jquave\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/","og_locale":"en_US","og_type":"article","og_title":"Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave","og_description":"Using the AVFoundation API, we are going to set up a capture session and make an app that allows us to use all the new fine-grained controls added to iOS 8. This includes manually controlling focus, exposure, and ISO. First off, we just need to set up a basic camera preview. In Part 1, we...","og_url":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/","og_site_name":"Jameson Quave","article_published_time":"2014-09-21T00:20:51+00:00","article_modified_time":"2014-09-21T00:31:22+00:00","author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/","url":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/","name":"Taking control of the iPhone camera in iOS 8 with Swift (Part 2) - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2014-09-21T00:20:51+00:00","dateModified":"2014-09-21T00:31:22+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Taking control of the iPhone camera in iOS 8 with Swift (Part 2)"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc","name":"Jameson Quave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","caption":"Jameson Quave"},"sameAs":["http:\/\/jamesonquave.com"],"url":"https:\/\/jamesonquave.com\/blog\/author\/jquave\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1264"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=1264"}],"version-history":[{"count":2,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":1270,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1264\/revisions\/1270"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}