Cocoa is hot

19 Apr 2008

I was playing with Cocoa a bit more today, looking to parse and display
information from the Apple Trailers feed. This
involved a few things, parsing the XML, loading the images and playing the
video. Let me just say, Cocoa makes this stupidly easy.

Pulling down the XML feed was a simple matter using the NSXMLDocument
object. I made one extension from a mactech article
here which
was to add the childNamed: method. Using this it’s really easy to pull the
nodes out of the XML tree and build my objects. I don’t have to deal with the
actual parsing and storing string values as the nodes are processed.

With that out of the way we move onto the image loading. If I tried to load up
all the images at the parsing of the document the application loading would be
too slow (trust me, I did this originally to make sure everything worked).
After a bit of poking this is also trivial to deal with in Cocoa. You can create
a NSImage with a reference to a URL using initByReferencingURL: which
will lazy load the image. In other words, only load the image when there is an
attempt to draw it. This works nicely with the IKImageBrowserView as, I
believe, it only loads images for what’s on screen. (If you didn’t want to lazy
load you could use initWithContentsOfURL: which will load the image
immediately.)

Displaying the images is trivial using the IKImageBrowserView as seen in my
previous
MyTube image wall article.

The last bit was displaying the movie. Again, this was pretty simple. I just
added a QTMovieView to my application then it’s just a matter of loading the
movie using the QTMoviemovieWithURL:error: method. This will load the
movie from the given URL. I can then set the movie into my view and play it. I
can also inform the movie view to maintain the aspect ratio of the movie by
calling setPreserveAspectRation: which is quite nice.

So, all in all, I must say, the more I play with Cocoa the more it seems they’ve
wrapped a lot of the tedious work up into a nice package for you. I wonder at
what point the bubble is going to burst for me?