cocoa

Objective-C Memory Management & Garbage Collection

Friday Q&A 2010-12-03: Accessors, Memory Management, and Thread Safety

Drawing Part of a UIImage

Happy Thanksgiving, and sorry for the relative lack of posts here lately. Things are crazier than ever and it’s been a challenge finding time to shower, let alone blog.

I do have something to share, today, though. No, it’s not the next chapter of OpenGL ES 2.0 for iOS. It’s a category that some of you may find useful: a method that allows you to draw only part of a UIImage rather than the entire thing.

On the Mac, NSImage has a handy instance method called drawInRect:fromRect:operation:fraction: that lets you specify exactly which part of an image to draw. On UIImage, we’ve only got the ability to draw the entire image either unless we drop down to Core Graphics calls. We don’t have a nice, easy, convenient way using just UIImage to draw a portion of the image it represents.

I needed this ability in an application I’m working on, so I hacked out the following category. At first glance, this may look to be inefficient, since we’re making a copy of the instance’s backing CGImage in order to create the sub-image, however I believe that CGImageCreateWithImageInRect() references the original image’s bitmap data. I haven’t confirmed that it doesn’t make a copy of the bitmap data, but the documentation certainly seems to imply it. Anyone know?

Anyway, here is the category; I’ve even commented the code more pedantically than is normal for me in case anyone might be confused about what’s going on. Improvements and bug fixes are, as always, welcome.

Read more on Drawing Part of a UIImage…

Cutting Large Images into Tiles for UIScrollView

If you ever need to display an obscenely large image on an iOS device using a UIScrollView, there’s a session available in the WWDC 2010 videos that will show you exactly how to do it without eating up all of your available memory. The basic idea is that you have multiple versions of your image stored at different scales, and you chop each of them up into tiles and use a CATiledLayer to display them. That way, you don’t have to maintain the entire obscenely large image in memory all at once and your app’s scroll performance stays snappy.

Of course, you have to get the images chopped up into tiles in order to use this technique.

There’s a great command-line open source tool called ImageMagick that will, among other things, chop an image up into tiles that can be used for this purpose. Unfortunately, I didn’t find ImageMagick very useful for really, really large images (over around 100 megs). The program would just sit and churn and fill up my hard drive with gigs of swap space without generating any tiles. It worked fine for smaller images, but I needed something that would work on big ones.

So, I went looking for an alternative tool without much luck. There are a handful of commercial tools that will do this, but the ones I found were for Windows. I know Photoshop has the ability to do this with slices, but I didn’t want to go down that route for a couple of reasons, not the least of which is that I don’t have a current version of Photoshop and don’t want to give Adobe any money.

After some fruitless Googling, I decided to take advantage of the fact that I’m a programmer, and I rolled my own app to do this. It was nice to be back in the Cocoa APIs, but I must admit that they feel a little krufty compared to the iOS APIs. Anyway, I call the program “Tile Cutter”. It’s not the most original name, I admit, but it was developed as an in-house product and didn’t need a witty title.

Tile Cutter is rather bare bones and was developed in about a half-day, but it seems to work well. I was able to slice up a 1.3 gig image (yes, really) into 400×400 chunks in about 15 minutes on my laptop. I would imagine I’m not the only person who needs to slice up images to use in UIScrollView, so I’ve decided to release Tile Cutter as a free utility (of course, with no warranties or whatnot). I will also be releasing the source code on GitHub once I’ve had a chance to clean up the code a little.

Here’s a screenshot of the main interface:
Screen shot 2010-10-08 at 3.25.23 PM.png

And the progress bars that display in a sheet when Tile Cutter’s churning away. You’ll actually only see the progress bars on very large images, however, since it operates quite quickly on more reasonably sized images.
Screen shot 2010-10-08 at 3.29.46 PM.png

Download Tile Cutter 1.0. Feedback, as always, is welcome.

©2008-2010 Jeff LaMarche.
http://iphonedevelopment.blogspot.com

Read more on Cutting Large Images into Tiles for UIScrollView…

How to Thwart iPhone IPA Crackers: Detection

Dealloc

Last week there was a bit of a Twitter in-fight in the iOS community over the “right” way to release your instance variables in dealloc. I think Rob actually started it, to be honest, but I probably shouldn’t be bringing that up.

Basically, several developers were claiming that there’s never a reason to set an instance variable to nil in dealloc, while others were arguing that you should always do so.

To me, there didn’t seem to be a clear and compelling winner between the two approaches. I’ve used both in my career. However, since we’re in the process of trying to decide which approach to use in the next edition of Beginning iPhone Development, I reached out to Apple’s Developer Tools Evangelist, Michael Jurewitz, to see if there was an official or recommended approach to handling instance variables in dealloc.

Other than the fact that you should never, ever use mutators in dealloc (or init, for that matter), Apple does not have an official recommendation on the subject.

However, Michael and Matt Drance of Bookhouse Software and a former Evangelist himself, had discussed this issue extensively last week. They kindly shared their conclusions with me and said it was okay for me to turn it into a blog post. So, here it is. Hopefully, I’ve captured everything correctly.

Read more on Dealloc…

setDrawsBackground, NSTextField and CALayers

Some of you might know that text rendered by a layer-backed NSTextField label (so, with Core Animation) on Mac OS X onto an alpha-transparent background (a layer with no direct opaque background, regardless of what that background may be, within the same layer) is drawn in a very peculiar way, a way which I will charitably characterize as “shitty”.

Read more on setDrawsBackground, NSTextField and CALayers…

Hold On A Minute

One of the big new features in MarsEdit 3 is a revamped media manager that allows you to easily browse local libraries from iPhoto, Aperture, etc.

Media Manager.png

This functionality is based largely on the great work of the iMedia project, which was spearheaded by Karelia to replicate Apple’s own ubiquitous media browsing interface, some variant of which is available in Pages, iWeb, Mail, etc.

Read more on Hold On A Minute…

MGSplitViewController for iPad

MGSplitViewController is an open source replacement for UISplitViewController, with various useful enhancements.

Here’s a demonstration video hosted on YouTube. It’s better if you watch it in full HD.

Donations

I wrote MGSplitViewController for my own use, but I’m making it available (as usual) for the benefit of the iOS developer community, because I care about this community and about the platform.

Read more on MGSplitViewController for iPad…

cocos2d

Google Author link
Page 1 of 812345678