{"id":855,"date":"2013-02-08T19:54:42","date_gmt":"2013-02-08T19:54:42","guid":{"rendered":"https:\/\/zogspat.tk\/blog\/?p=855"},"modified":"2013-08-17T11:12:01","modified_gmt":"2013-08-17T11:12:01","slug":"doing-battle-with-imagepickercontroller-and-orientation","status":"publish","type":"post","link":"https:\/\/the-plot.com\/blog\/?p=855","title":{"rendered":"Doing Battle with imagePickerController and Orientation"},"content":{"rendered":"<p>I am in the home straight &#8211; hopefully &#8211; with my new app which is, unsurprisingly enough, photography related. It&#8217;s an order of magnitude of complexity above the weight tracker, and has been a real challenge to get through.<\/p>\n<p>The interface to the \u00a0ALAssetsLibrary isn&#8217;t without its foibles, including the fact that, when used in conjunction with the image picker, there is a fair amount of work to be done by hand.<\/p>\n<p>One of the bigger surprises is image orientation, which you have to add yourself.<\/p>\n<p>Perhaps this incantation may come in handy for you:<\/p>\n<pre>UIImage\u00a0*image = [info\u00a0objectForKey:UIImagePickerControllerOriginalImage];<\/pre>\n<pre>int\u00a0imOrNum = image.imageOrientation;<\/pre>\n<pre>...<\/pre>\n<div>\n<pre>int\u00a0orientMetaVal =\u00a00;<\/pre>\n<pre>\u00a0 \u00a0\u00a0if\u00a0(imOrNum ==\u00a01)<\/pre>\n<pre>\u00a0 \u00a0 {<\/pre>\n<pre>\u00a0 \u00a0\u00a0\/\/ landscape, vol buttons up<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 orientMetaVal =\u00a03;<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0landscapeSnap\u00a0=\u00a0TRUE;<\/pre>\n<pre>\u00a0 \u00a0 }<\/pre>\n<pre>\u00a0 \u00a0\u00a0else\u00a0if\u00a0(imOrNum ==\u00a03)<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/portrait, home button down<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 orientMetaVal =\u00a06;<\/pre>\n<pre>\u00a0 \u00a0\u00a0else\u00a0if\u00a0(imOrNum ==0)<\/pre>\n<pre>\u00a0 \u00a0 {<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ landscape, vol buttons down<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 orientMetaVal =\u00a00;<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0landscapeSnap\u00a0=\u00a0TRUE;<\/pre>\n<pre>\u00a0 \u00a0 }<\/pre>\n<pre>\u00a0 \u00a0\u00a0else\u00a0if\u00a0(imOrNum ==\u00a02)<\/pre>\n<pre>\u00a0 \u00a0 {<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ portrait, home button up<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ 5 is weird - thumbnail right, image inverted.<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 orientMetaVal =\u00a08;<\/pre>\n<pre>\u00a0 \u00a0 }<\/pre>\n<pre>...<\/pre>\n<pre>[tmpMetadataDic\u00a0setObject:[NSNumber\u00a0numberWithInt:orientMetaVal]\u00a0forKey:@\"Orientation\"];<\/pre>\n<\/div>\n<div>So the landscape trueness is just something that I use when I&#8217;m previewing the image that has just been taken. You then need to set various bits and pieces in a NSMutableDictionary, such as the orientation, which eventually gets written back with the file as a\u00a0parameter to<\/div>\n<div>\n<pre>[yourInstantiatedAl\u00a0writeImageToSavedPhotosAlbum:[nicePicture\u00a0CGImage]<\/pre>\n<pre>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0metadata:tmpMetadataDic<\/pre>\n<p>One other thing to look out for is the requirement to add in all of the vanilla EXIF data &#8211; ISO, shutter, etc. That&#8217;s actually pretty easy. I initialise the dictionary of metadata with this:<\/p>\n<\/div>\n<pre>NSMutableDictionary\u00a0*tmpMetadataDic = [info\u00a0objectForKey:UIImagePickerControllerMediaMetadata];<\/pre>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I am in the home straight &#8211; hopefully &#8211; with my new app which is, unsurprisingly enough, photography related. It&#8217;s an order of magnitude of complexity above the weight tracker, and has been a real challenge to get through. The &hellip; <a href=\"https:\/\/the-plot.com\/blog\/?p=855\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-855","post","type-post","status-publish","format-standard","hentry","category-ios-development"],"_links":{"self":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=855"}],"version-history":[{"count":3,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/855\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/855\/revisions\/941"}],"wp:attachment":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}