Learn Objective-C on the Mac - Mark Dalrymple [135]
We’ve touched on this before, but it’s worth repeating: even though we’re encouraging you to use GC in all your applications, there may be times when you can’t. You may find that you need to start linking with a framework or library that doesn’t work well with GC, or that you want to port your application to iPhone, which (at the time of this writing anyway) doesn’t include GC at all. Taking the simple step of writing your accessors from the beginning in a way that will work in a non-GC situation will make such transitions that much easier. With that said, writing your own accessors that properly deal with memory management issues in a non-GC environment can be trickier that you might imagine. If you’re not 100 percent sure of how to proceed, you can find a detailed discussion of these issues in Learn Objective-C on the Mac, by Mark Dalrymple and Scott Knaster (Apress, 2009).
Drawing a Bitmap
Now let’s move on to the “meat” of the LOLView class: drawing the image and superimposing some text. Let’s start with the image, which we can copy into place quite quickly by adding a few lines to the drawRect: method that was put in place when you created the class:
The first thing we’re doing here is creating a rect called srcImageRect with origin (0,0) and size equal to the image’s size. Then we send a message to the image itself, telling it to draw the portion of the image specified by srcImageRect (in this case, the entire image) into the current graphics context, located at the origin of our view’s bounds rect. In short, the entire image is copied, and its lower left-hand corner will be located precisely at the view’s lower left-hand corner. The drawing method we’re using here also lets us specify an operation, which determines how transparencies in the source and destination images are merged, as well as an integer between 0.0 and 1.0, which acts as an overall alpha level for the entire image. Any value below 1.0 makes the image somewhat transparent; all the way down to 0.0 makes the image completely invisible.
Now, Build & Run your app, and let’s take a look at what we’ve got so far. Your app opens, showing you a mostly blank window with the image well and text field at the bottom. Find a nice LOLcat-friendly image somewhere, and drag it into the image well, which will show you something like you see in Figure 13-10.
Figure 13-10. Not much LOL here.
Hey, that’s not very satisfying! All we’re seeing is the lower left-hand corner of the picture we dragged in! If only there were some way we could see the whole image...
Let It Scroll
Well, of course there is. Cocoa includes a class called NSScrollView that will help us out here. By putting a view inside of an NSScrollView, you get access to horizontal and vertical scrollbars that the user can use to slide the view around. The scroll view takes care of all the hard work. The drawing code for any views you put in there doesn’t need to change at all! Putting the LOLView into an NSScrollView is surprisingly simple. We just need to add a bit of code and make a couple of adjustments in Interface Builder.
The code we need to add is actually one more piece of the setImage: method. What we’re going to do is resize the LOLView each time a new image is set, so that the view’s size matches the image’s size. Later, when the LOLView is enclosed in an NSScrollView, the scrollview will notice the new view size, and automatically re-render everything including the scrollbars. The new code looks like this:
Now, go back to Interface Builder to prepare the scrollview itself. Select the LOLView, then select Layout➤Embed Objects In➤Scroll View from the menu. Your LOLView is now wrapped in a scroll view, but the positioning and sizing are a little off. Move the scroll