<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iPhone Programming Tutorials &#187; UIImagePickerController</title>
	<atom:link href="/tag/uiimagepickercontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Update: ELCImagePickerController</title>
		<link>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/</link>
		<comments>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 00:23:14 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2897</guid>
		<description><![CDATA[I recently spent some  time with <a href="https://github.com/elc/ELCImagePickerController">ELCImagePickerController</a>.  For those of you who&#8217;ve worked with UIImagePickerController, you might have noticed one of its major drawbacks: you can only select one photo at a time.  ELCImagePickerController solves this issue by cloning the UI of UIImagePickerController, but with the added bonus of allowing you to select multiple assets.  Collin Ruffenach (<a href="http://twitter.com/cruffenach">@cruffenach</a>), who authored the the first version of the picker, has done an awesome job of making  ...]]></description>
				<content:encoded><![CDATA[<p>I recently spent some  time with <a href="https://github.com/elc/ELCImagePickerController">ELCImagePickerController</a>.  For those of you who&#8217;ve worked with UIImagePickerController, you might have noticed one of its major drawbacks: you can only select one photo at a time.  ELCImagePickerController solves this issue by cloning the UI of UIImagePickerController, but with the added bonus of allowing you to select multiple assets.  Collin Ruffenach (<a href="http://twitter.com/cruffenach">@cruffenach</a>), who authored the the first version of the picker, has done an awesome job of making ELCImagePickerController look, feel, and behave like a native image picker.  For this post I&#8217;m going to go through some recent improvements I made to ELCImagePickerController and pass along some lessons learned from working with the AssetsLibrary Framework.</p>
<p>This is my first foray into the AssetsLibrary Framework which was introduced with iOS 4.0.  If you&#8217;re not already familiar with the framework, I highly suggest checking out both of these posts:</p>
<h3><a title="Asset Libraries and Blocks in iOS 4" rel="bookmark" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/">Asset Libraries and Blocks in iOS 4</a></h3>
<h3><a title="Cloning UIImagePickerController using the Assets Library Framework" rel="bookmark" href="/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/">Cloning UIImagePickerController using the Assets Library Framework</a></h3>
<p>These should give you a solid picture of how to work with ALAssetLibrary, ALAssetGroup, and ALAsset.</p>
<p>One of the obvious changes I&#8217;ve made to the project is a reorganization of the code.  Classes are broken out for clarity as you can see here:</p>
<p>Old Tree:<br />
<img class="alignnone" src="/wp-content/uploads/2011/03/old_tree.png" alt="" width="289" height="191" /><br />
New Tree:<br />
<img class="alignnone" src="/wp-content/uploads/2011/03/Screen-shot-2011-03-01-at-5.14.49-PM.png" alt="" width="246" height="309" /></p>
<div>I&#8217;ve also renamed several classes to avoid confusion w/ Apple&#8217;s and ELC&#8217;s respective classes.</div>
<p>On major change to the project is how you present the new ELCImagePickerController.  This is mainly due to an issue we uncovered with a redundant call to [super init] which was causing a substantial memory leak.  Here&#8217;s the new way of showing the image picker:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:@&quot;ELCAlbumPickerController&quot; bundle:[NSBundle mainBundle]];ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
&nbsp;
[albumController setParent:elcPicker];
&nbsp;
[elcPicker setDelegate:self];
&nbsp;
ELCImagePickerDemoAppDelegate *app = (ELCImagePickerDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
&nbsp;
[app.viewController presentModalViewController:elcPicker animated:YES];
&nbsp;
[elcPicker release];
&nbsp;
[albumController release];</pre></td></tr></table></div>

<p>One interesting optimization I was able to make was on the loading of large albums which was previously taking several seconds.  Since ALAssetGroup uses a block to enumerate assets, I just fire off a reloadTable call after a short delay:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="obj-c" style="font-family:monospace;">// Show partial list while full list loads
&nbsp;
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:.5];</pre></td></tr></table></div>

<p>Then when the block is finished enumerating, it&#8217;ll call reloadTable as well.  On an album with roughly 1500 photos, it was pretty tough to reach the bottom of the tableview before the block finished enumerating.  So this is a pretty decent solution to the issue.  Originally, I experimented with lazy loading the assets as the user scrolled through the table, but ultimately wasn&#8217;t able to get the performance I wanted out of it.</p>
<p>This release of ELCImagePickerController should perform faster, and with a smaller memory footprint than before.  I hope you enjoy it.</p>
<p>You can follow me on twitter <a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a> or get in touch with us at <a href="http://www.elctech.com">http://www.elctech.com</a></p>
<p>ps. for an extra bonus, check out [ELCAsset toggleSelection];</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/03/03/update-elcimagepickercontroller/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Cloning UIImagePickerController using the Assets Library Framework</title>
		<link>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/</link>
		<comments>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 22:51:05 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Custom UITableViewCells]]></category>
		<category><![CDATA[ELCImagePickerController]]></category>
		<category><![CDATA[nsoperation]]></category>
		<category><![CDATA[UIGestureRecognizers]]></category>
		<category><![CDATA[UIImagePickerController]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2338</guid>
		<description><![CDATA[Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow  ...]]></description>
				<content:encoded><![CDATA[<p>Hello iCoders. This is a follow up post to my initial post on the <a title="The Assets Library and Blocks in iOS 4.0" href="/2010/07/08/asset-libraries-and-blocks-in-ios-4/" target="_blank">Assets Library Framework and Blocks</a>. We came across an interesting problem when working on the application for <a href="http://animoto.com">Animoto.com</a>. They have had an <a title="Animoto in the App Store" href="http://itunes.apple.com/us/app/animoto-videos/id300033126?mt=8" target="_blank">app in the store</a> since the very early days of the app store, and one of our biggest struggles has been creating an interface to allow users to select multiple photos from their photo album for a slideshow. While the iPhone photos application allows for this, the UIImagePicker does not. With the release of the Assets Library framework we can now recreate the experience of the UIImagePicker, with some additional custom functionality to fit our needs. As a result we created a new cloned version of the UIImagePicker that allows for multiple photo selection just like the photos app. We have decided to open source the controller for other developers to use. This post will explain the process of creating the new image picker as well as the method of incorporating it into your code.</p>
<h2>The ELCImagePickerController</h2>
<p>The ELCImagePickerController is only possible because of the newly introduced Assets Library framework. This framework gives you raw (more or less) access to the photo and video elements of a user. We looked at UIImagePickerController and saw a lot of weaknesses with it. You can only select 1 photo at a time, and even in Apple&#8217;s photo app, where you can choose several pictures at a time, you can&#8217;t use multi touch to do your selection. To solve these problems we rolled our own solution that works very closely to UIImagePickerController.</p>
<h3>How to use it</h3>
<p>First I am going to explain using the picker since to many people the process of creating it won&#8217;t be very interesting. The image picker is created and displayed in a very similar manner to the UIImagePickerController. The sample application that is part of the GitHub project, where I distribute the controller, shows its use, but I will go into detail here. To display the controller you instantiate it and display it modally like so.</p>
<pre>ELCImagePickerController *controller = [[ELCImagePickerController alloc] initImagePicker];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];</pre>
<p>The ELCImagePickerController will return the select images back to the ELCImagePickerControllerDelegate. The delegate contains to methods very similar to the UIImagePickerControllerDelegate. Instead of returning one dictionary representing a single image the controller sends back an array of similarly structured dictionaries. The two delegate methods are:]</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>elcImagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingMediaWithInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>info;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>elcImagePickerControllerDidCancel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker;</pre></td></tr></table></div>

<h2>GIT Hub</h2>
<p>You can find this project <a title="ELCImagePickerController on GITHub" href="http://github.com/elc/ELCImagePickerController" target="_blank">now on GitHub</a>. Please let me know any issues you may have and look for future releases with feature enhancements</p>
<h2>General Structure</h2>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png"><img class="aligncenter size-full wp-image-2444" title="ELCImagePickerControllerArchitecture" src="/wp-content/uploads/2010/10/ELCImagePickerControllerArchitecture1.png" alt="" width="526" height="306" /></a></p>
<p style="text-align: left;">The ELCImagePicker is a collection of UITableViewControllers that are placed inside a UINavigationController. While the ELCImagePickerController is actually 5 separate custom classes I have written, I have put them all within a single header and main. I chose this to make the classes that were required to be imported into a project when using this as few as possible.  While usually when presenting a UINavigationController you would create one yourself in code and use the initWithRootViewController method, in this case we have created a UINavigationController subclass called ELCImagePickerController which does all this behind the scenes. In the end all a developer has to do is use the initImagePicker method and present the controller modally. This lets the class match the functionality of UIImagePickerController closer. You can see the Header and Main for the ELCImagePickerController class on the next page.</p>
<p><a href="http://vimeo.com/15666311">ELCImagePickerControllerDemo</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Asset Libraries and Blocks in iOS 4</title>
		<link>http://icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/</link>
		<comments>http://icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 20:29:31 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Assets Library]]></category>
		<category><![CDATA[Blocks]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iOS4]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2068</guid>
		<description><![CDATA[
iOS 4 presented a million billion new API&#8217;s by Apple&#8217;s count, but for this post I am going to show a quick demo of the new Assets Library API where you can now get to users photos and videos with more access than ever before. This new API relies on the new programming device called Blocks that has been introduced with iOS 4. Blocks are used through many of the new API&#8217;s as a kind of extended @selector. We will  ...]]></description>
				<content:encoded><![CDATA[<div>
<p>iOS 4 presented a million billion new API&#8217;s by Apple&#8217;s count, but for this post I am going to show a quick demo of the new Assets Library API where you can now get to users photos and videos with more access than ever before. This new API relies on the new programming device called Blocks that has been introduced with iOS 4. Blocks are used through many of the new API&#8217;s as a kind of extended @selector. We will look into this new development device and make a small project to create our own UIImagePickerController. In the end we are going to create a tableview that is filled with all the photos within our library. Check out the video below or follow the steps typed out below.</p>
<h2><span style="color: #ff6600;">Screencast</span></h2>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="375" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=13187660&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="600" height="375" src="http://vimeo.com/moogaloop.swf?clip_id=13187660&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://vimeo.com/13187660">Using the Assets Library API in iOS 4</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2><span style="color: #ff6600;">Creating the Project</span></h2>
<p>Create a new project in xCode. Make it for the iPhone since this is an iOS 4 framework. A view based application will create a view controller for us, so lets use that. Call your project MyImagePicker.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Make-Project11.png"><img class="size-full wp-image-2075 aligncenter" title="Make Project" src="/wp-content/uploads/2010/07/Make-Project11.png" alt="" width="478" height="391" /></a></p>
<h2><span style="color: #ff6600;">Adding in the framework</span></h2>
<p>This project will revolve around taking advantage of the AssetsLibrary framework introduced with iOS 4. Due to this you must have the newest xCode and iPhone SDK installed with iOS 4.0 support. If this is done you should be able to expand the frameworks folder of your project. Right click on UIKit.framework and select &#8220;Reveal in Finder&#8221;. This will open up the folder that contains all of the frameworks you can use in an iOS 4 project. Find AssetsLibrary.framework and drag into the Frameworks folder of your project. Once dragged in make sure that the Copy items into destination folder options is deselected and that it is set to recursively create groups for any added folders.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Import-Options1.png"><img class="size-full wp-image-2078 aligncenter" title="Import Options" src="/wp-content/uploads/2010/07/Import-Options1.png" alt="" width="416" height="388" /></a></p>
<h2><span style="color: #ff6600;">Using the framework with blocks</span></h2>
<p>iOS 4.0 introduces a totally new programming concept for Objective C developers to take advantage of when creating their own code and when using Apple&#8217;s API. Blocks in the most simple terms are Objective C objects that are methods. A block uses the special character ^ to denote its beginning and is a required parameter of many of the API&#8217;s introduced within iOS 4.0. Apple has some great documentation on blocks, and how they conceptually fit into the current objective C landscape. You can see their concise but effective overview <a title="Blocks: A short practical guide" href="http://developer.apple.com/iphone/library/featuredarticles/Short_Practical_Guide_Blocks/index.html" target="_blank">here</a>. For our purposes I am just going to review the simple syntax of a block.</p>
<p>Before we dive into blocks lets take a look at the AssetsLibrary framework methods that will require them. The general flow of the object access we will be doing is as follows.<br />
<a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-01-at-4.48.20-PM1.png"></a></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-01-at-4.48.20-PM1.png"><img class="size-full wp-image-2081 aligncenter" title="Screen shot 2010-07-01 at 4.48.20 PM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-01-at-4.48.20-PM1.png" alt="" width="395" height="615" /></a></p>
<p>We are going to create an ALAssetsLibrary and call a method on it that will enumerate through all the ALAssetsGroups it has access to. For every ALAssetsGroup we will call a method on it which will enumerate through the ALAssets it has access to. We will save each ALAsset into an array we will use to populate our tableview. The two different enumerations we are going to perform is where the blocks will come in. But lets first set up our view controller appropriately.</p>
<h2><span style="color: #ff6600;">MyImagePickerViewController.h</span></h2>
</div>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;UIKit/UIKit.h&amp;gt;;</span>
<span style="color: #6e371a;">#import &amp;lt;AssetsLibrary/AssetsLibrary.h&amp;gt;;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyImagePickerViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
	IBOutlet UIActivityIndicatorView <span style="color: #002200;">*</span>activity;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>assets;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITableView <span style="color: #002200;">*</span>tableview;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIActivityIndicatorView <span style="color: #002200;">*</span>activity;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>We are going be using the assets array here to hold the assets that we are going to pull using the AssetsLibrary Framework.</p>
<p>Now open up MyImagePickerViewController.xib and drag in a UITableView and a UIActivityIndicatorView. With these in place connect them to the IBOutlets you created for this class. Also make sure to connect the UITableViewDataSource and UITableViewDelegate to the MyImagePickerViewController as well. With that done we can start to use the AssetsLibrary Framework.</p>
<h2><span style="color: #ff6600;">Using the Asset Library</span></h2>
<p><span style="color: #ff6600;"><span style="color: #000000;">Now we are going to use our AssetsLibrary framework. This will be done within the viewDidLoad method of your MyImagePickerViewController.m. Put the following code in there.</span></span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #2400d9;">1</span> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">2</span>
<span style="color: #2400d9;">3</span>    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">4</span>    <span style="color: #002200;">&#91;</span>activity startAnimating<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">5</span>
<span style="color: #2400d9;">6</span>    <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>assetEnumerator<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> ALAsset <span style="color: #002200;">*</span>, NSUInteger, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span>ALAsset <span style="color: #002200;">*</span>result, NSUInteger index, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">7</span>	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">8</span>		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See Asset: %@&quot;</span>, result<span style="color: #002200;">&#41;</span>;
<span style="color: #2400d9;">9</span>		<span style="color: #002200;">&#91;</span>assets addObject<span style="color: #002200;">:</span>result<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">10</span>
<span style="color: #2400d9;">11</span>	<span style="color: #002200;">&#125;</span>
<span style="color: #2400d9;">12</span>    <span style="color: #002200;">&#125;</span>;
<span style="color: #2400d9;">13</span>
<span style="color: #2400d9;">14</span>    <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">^</span>assetGroupEnumerator<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> ALAssetsGroup <span style="color: #002200;">*</span>, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span>  <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span>ALAssetsGroup <span style="color: #002200;">*</span>group, <span style="color: #a61390;">BOOL</span> <span style="color: #002200;">*</span>stop<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">15</span>	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>group <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">16</span>		<span style="color: #002200;">&#91;</span>group enumerateAssetsUsingBlock<span style="color: #002200;">:</span>assetEnumerator<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">17</span>	<span style="color: #002200;">&#125;</span>
<span style="color: #2400d9;">18</span>
<span style="color: #2400d9;">19</span>
<span style="color: #2400d9;">20</span>	<span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">21</span>	<span style="color: #002200;">&#91;</span>self.activity stopAnimating<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">22</span>	<span style="color: #002200;">&#91;</span>self.activity setHidden<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">23</span>    <span style="color: #002200;">&#125;</span>;
<span style="color: #2400d9;">24</span>
<span style="color: #2400d9;">25</span>    assets <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">26</span>    library <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ALAssetsLibrary alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">27</span>    <span style="color: #002200;">&#91;</span>library enumerateGroupsWithTypes<span style="color: #002200;">:</span>ALAssetsGroupAlbum
<span style="color: #2400d9;">28</span>					   usingBlock<span style="color: #002200;">:</span>assetGroupEnumerator
<span style="color: #2400d9;">29</span>					 failureBlock<span style="color: #002200;">:</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
<span style="color: #2400d9;">30</span>						 NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Failure&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #2400d9;">31</span>					 <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #2400d9;">32</span> <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We are going to be going though this code line by line. On line 4, we start animating our activity indicator. I will explain why we have to do that a little bit later. Lines 6 &#8211; 23 are the lines that will  declare the two blocks that we will use to fill our tableview with pictures. We are going to declare two blocks to use as parameters to methods within a ALAssetLibrary object and a ALAssetGroup object. The first method call we will make will be on our ALAssetLibrary object. So for the moment lets skip passed the block declarations and look at line 25. On line 25 we instantiate our NSMutableArray to hold the ALAssets that we will pull out from the AssetsLibrary. On line 26 we will create our ALAssetsLibrary object. We will call a single method on this object to loop through all of the assets in a library. The method is:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> enumerateGroupsWithTypes<span style="color: #002200;">:</span>usingBlock<span style="color: #002200;">:</span>failureBlock<span style="color: #002200;">:</span></pre></td></tr></table></div>

<p>This method takes in 3 parameters. An ALAssetGroup type, a block to be performed on each group and a block to be performed on failure. Lets talk about block syntax. A block is a native Objective C object. It is a subclass of NSObject. With that said it is also an Objective C method. An objective C method is composed of three things. A return type, a parameter list and a piece of code to be executed. We are going to pass the assetsGroupEnumerator block object that declare on line 14 into this method. Lets take a look at how the block is declared.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2010/07/Screen-shot-2010-07-06-at-1.45.15-PM1.png"><img class="size-full wp-image-2101 aligncenter" title="Screen shot 2010-07-06 at 1.45.15 PM" src="/wp-content/uploads/2010/07/Screen-shot-2010-07-06-at-1.45.15-PM1.png" alt="" width="582" height="124" /></a></p>
<p>You begin by specifying a return type for the block. Void in this case. Next you enclose a name for the block which is proceeded by a carrot ^ character. This is the special character Apple has decided on to denote blocks. You begin block names with blocks as well as the actual declaration of what a block is. This name is wrapped in parenthesis. This is followed by the parameter list that the block will accept. In this case it is a ALAssetsGroup object and a Boolean indicating whether to stop. Now I did not come up with these parameters by myself. Since I will be passing this block into an ASAssetLibrary the documentation will tell us what will be passed into the block. ALAssetsLibrary objects documentation can be seen <a title="ALAssetsLibrary API Reference" href="http://developer.apple.com/iphone/library/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html" target="_blank">here</a>. With that all said, lets take a look at the code that will actually compose this block. Starting on line 15 we will make sure that we are seeing a valid group. If the ALAssetsGroup object that we are passed is valid then we will call another enumeration on the group which will enumerate through assets. This enumeration method also requires a block. We declared this block above. This block is called assetEnumerator. Once again we retrieved the parameters list from the documentation of the object asking for, in this case an <a title="ALAssetsGroup API" href="http://developer.apple.com/iphone/library/documentation/AssetsLibrary/Reference/ALAssetsGroup_Class/Reference/Reference.html#//apple_ref/occ/instm/ALAssetsGroup/enumerateAssetsWithOptions:usingBlock:" target="_blank">ALAssetsGroup</a>.</p>
<p>For this block, which begins on line 6, the block is passed an ALAsset object, an index and a stopping condition boolean. Within the code of the block we will ensure that the returned ALAsset is valid and if it is we will add it to our array which will hold all of our assets. With this block declared we pass it into the call to our ALAssetsLibrary object on line 16.  Once every group has been enumerated and our array is filled with all of our ALAssets we will tell the tableview to reload its data and get rid of our UIActivityIndicatorView.</p>
<p>With all of this done all that remains if filling in the required UITableViewDataSource methods. The tableview will be filled with the assets we collected. ALAsset objects are cool in that they include a method called -thumbnail which returns a CGImageRef to a thumbnail we will use. I won&#8217;t explain the development of these methods any further since they are pretty straight forward.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Customize the number of sections in the table view.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Customize the number of rows in the table view.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>assets count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Customize the appearance of table view cells.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
	ALAsset <span style="color: #002200;">*</span>asset <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>assets objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>cell.imageView setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>asset thumbnail<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>cell.textLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Photo %d&quot;</span>, indexPath.row<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
	</channel>
</rss>
