{"title":"Accessing Image Properties Without Loading the Image","html_url":"https://deallocatedobjects.com/posts/accessing-image-properties-without-loading-the-image-into-memory","source_url":"http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/","markdown_content":"I see this type of stuff a lot in other people's code:\r\n\r\n``` objc\r\nUIImage *image = [UIImage imageWithContentsOfFile:@\"lolcats.jpg\"];\r\nCGSize imageSize = image.size;\r\n```\r\n\r\nIt's certainly not *wrong*, but you are incurring a huge performance and memory usage hit just to get the width and height of an image. Enter the `CGImageSource...` methods.\r\n\r\n> As of iOS 4, the SDK includes a better solution in the form of the `CGImageSource...` set of functions, which have been available on the Mac since forever. These functions allow you to access certain image metadata without having to load the actual pixel data into memory.\r\n\r\n[Ole Begemann](https://twitter.com/#!/olebegemann) has put together [a great article](http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/) explaining the ways around such performance hits, and how you can get pretty much any metadata you could want about an image, without incurring a performance or memory usage hit.","html_content":"<p>I see this type of stuff a lot in other people’s code:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">UIImage </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">*</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">image </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> [UIImage </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">imageWithContentsOfFile:</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"lolcats.jpg\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">];</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">CGSize</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> imageSize </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> image.size;</span></span></code></pre></figure>\n<p>It’s certainly not <em>wrong</em>, but you are incurring a huge performance and memory usage hit just to get the width and height of an image. Enter the <code>CGImageSource...</code> methods.</p>\n\n<blockquote>\n<p>As of iOS 4, the SDK includes a better solution in the form of the <code>CGImageSource...</code> set of functions, which have been available on the Mac since forever. These functions allow you to access certain image metadata without having to load the actual pixel data into memory.</p>\n</blockquote>\n\n<p><a href=\"https://web.archive.org/web/20110820025141/https://twitter.com/olebegemann\">Ole Begemann</a> has put together <a href=\"http://oleb.net/blog/2011/09/accessing-image-properties-without-loading-the-image-into-memory/\">a great article</a> explaining the ways around such performance hits, and how you can get pretty much any metadata you could want about an image, without incurring a performance or memory usage hit.</p>","created_at":"2013-11-16T05:10:25.646Z","updated_at":"2013-11-16T05:10:25.646Z"}