{"id":6,"date":"2008-03-27T12:55:06","date_gmt":"2008-03-27T17:55:06","guid":{"rendered":"http:\/\/developer.casgrain.com\/?p=6"},"modified":"2009-05-18T11:56:22","modified_gmt":"2009-05-18T16:56:22","slug":"pimp-my-ikimagebrowserview-slide-show","status":"publish","type":"post","link":"http:\/\/developer.casgrain.com\/?p=6","title":{"rendered":"Pimp my IKImageBrowserView: Slide Show"},"content":{"rendered":"<p>After listening to <a href=\"http:\/\/www.speirs.org\/\">Frasier Speirs<\/a> on <a href=\"http:\/\/www.macdevnet.com\/index.php\/shows\/latenightcocoa\/37-latenightcocoa\/220-lnc027\">Late Night Cocoa<\/a>, I decided to take a look at <a href=\"http:\/\/developer.apple.com\/graphicsimaging\/imagekit\/\">ImageKit<\/a>, a new Leopard-only technology that contains the most interesting parts of iPhoto.<\/p>\n<p\/>\nI had a need for an Image Browser, so I tried the (very well-written) <a href=\"http:\/\/developer.apple.com\/documentation\/GraphicsImaging\/Conceptual\/ImageKitProgrammingGuide\/ImageBrowser\/chapter_4_section_1.html#\/\/apple_ref\/doc\/uid\/TP40004907-CH5-SW1\">Browsing Images Programming Guide<\/a>. At the tutorial&#8217;s end, I had a &#8220;Browse Images&#8221; application:<\/p>\n<p><div align=\"center\"><a href=\"http:\/\/www.labecasse.com\/philippe\/images\/browse-images.png\" width=\"577\" height=\"486\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.labecasse.com\/philippe\/images\/browse-images-preview.png\" width=\"288\" height=\"243\" \/><\/a><\/div>\n<\/p>\n<p>If you haven&#8217;t done so, you can do <a href=\"http:\/\/developer.apple.com\/documentation\/GraphicsImaging\/Conceptual\/ImageKitProgrammingGuide\/ImageBrowser\/chapter_4_section_1.html#\/\/apple_ref\/doc\/uid\/TP40004907-CH5-SW1\" target=\"_blank\">this tutorial too<\/a>. I will wait until you&#8217;re done&#8230;<\/p>\n<p\/>\n&#8230;<\/p>\n<p\/>\nAll done? Great. I wanted some extras in &#8220;Browse Images&#8221;, for example a slideshow and a filter. There is a guide to build a slideshow application, but it makes you write a separate application. We already have &#8220;Browse Images&#8221;, surely we can re-use it?<\/p>\n<h3>Adding a slideshow<\/h3>\n<p>The <a href=\"http:\/\/developer.apple.com\/documentation\/GraphicsImaging\/Reference\/IKSlideShow\/IKSlideshow_Reference.html\">IKSlideShow<\/a> class is very simple. One call makes it all happen:<\/p>\n<pre>\n[[IKSlideshow sharedSlideshow] runSlideshowWithDataSource: (id<IKSlideshowDataSource>) self inMode: IKSlideshowModeImages options: nil]\n<\/pre>\n<p>The objects have to implement the <tt><a href=\"http:\/\/developer.apple.com\/documentation\/GraphicsImaging\/Reference\/IKSlideShowDataSource\/IKSlideshowDataSource_ref.html\">IKSlideshowDataSource<\/a><\/tt> protocol, which requires at least two methods:<\/p>\n<pre>\n\t- (NSUInteger) numberOfSlideshowItems\n\t- (id) slideshowItemAtIndex: (NSUInteger) index\n<\/pre>\n<p>That looks familiar. For the <tt>IKImageBrowserDataSource<\/tt> protocol, we already implemented similar methods:<\/p>\n<pre>\n- (int) numberOfItemsInImageBrowser: (IKImageBrowserView*) view\n{\n\treturn [mImages count];\n}\n\n- (id) imageBrowser: (IKImageBrowserView*) view itemAtIndex: (int) index\n{\n\treturn [mImages objectAtIndex: index];\n}\n<\/pre>\n<p>The only difference is the objects returned by <tt>(id) imageBrowser: itemAtIndex:<\/tt> had to implement the <tt>IKImageBrowserItem<\/tt> protocol, but they still returned an <tt>NSString*<\/tt>, a path to the image file.<\/p>\n<p\/>\n<i>(Another difference is the objects could be of more image types, but we will leave that for another time.)<\/i><\/p>\n<h3> Implementing the <tt>IKSlideshowDataSource<\/tt> protocol<\/h3>\n<p>In your <tt>ImageBrowserController<\/tt> implementation, add the missing methods to make the slide show work:<\/p>\n<pre>\n- (NSUInteger) numberOfSlideshowItems\n{\n\treturn [mImages count];\n}\n\n- (id) slideshowItemAtIndex: (NSUInteger) index\n{\n\tint i = index % [mImages count];\n\n\treturn [[mImages objectAtIndex: i] path]; \/\/ path? Where did that come from?\n}\n<\/pre>\n<p>The first method is the same as before, while the second one returns not a <tt>MyImageObject<\/tt> (what is stored in <tt>mImages<\/tt>), but the path of that object. To get this path, simply add an accessor to <tt>MyImageObject<\/tt>:<\/p>\n<pre>\n- (NSString*) path \n{ \n\treturn mPath;\n}\n<\/pre>\n<p><i>(Also add the prototype to the interface, lest you get a warning&#8230;)<\/i><\/p>\n<h3>Tying it together in Interface Builder<\/h3>\n<p>Now that our controller conforms to the <tt>IKSlideshowDataSource<\/tt> protocol, we need a way to start the slide show. To this end, create an <tt>IBAction<\/tt> in the controller&#8217;s interface, and implement it like this:<\/p>\n<pre>\n- (IBAction) startSlideshow: (id) sender\n{\n\tif ([mImages count] > 0)\n\t{\n\t\t[[IKSlideshow sharedSlideshow] runSlideshowWithDataSource: (id<IKSlideshowDataSource>) self inMode: IKSlideshowModeImages options: nil];\n\t}\n}\n<\/pre>\n<p>Create a button in Interface Builder, and Control-drag it to your controller to connect it to the <tt>startSlideShow:<\/tt> action (IB 3 should have automatically picked up the change in your header file and offer you the new action).<\/p>\n<p>\nBuild and run. You should be able to browse pictures like before, and pressing on the &#8220;Slideshow&#8221; button should start a full-screen slideshow, complete with index sheet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After listening to Frasier Speirs on Late Night Cocoa, I decided to take a look at ImageKit, a new Leopard-only technology that contains the most interesting parts of iPhoto. I had a need for an Image Browser, so I tried the (very well-written) Browsing Images Programming Guide. At the tutorial&#8217;s end, I had a &#8220;Browse [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,5],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-graphics","category-leopard","category-macosx"],"_links":{"self":[{"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6"}],"version-history":[{"count":2,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":28,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions\/28"}],"wp:attachment":[{"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/developer.casgrain.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}