Help Me With iPhone Dev Graphics Question, Please

Usually when I learn a new programming language or framework, I am plagued by the fact that I can’t think of anything to build with it, or I can only think of things that are too difficult. With iPhone development, I have two ideas for apps, both of which should be fairly easy to write. I’ve gotten a good start on the first app, but now I’ve hit a roadblock, and I’ve been stuck here for a while. I thought I’d ask for help.

Obviously I can’t disclose too much of what the app does, since I do eventually want to sell it in the App Store. Essentially, it allows for a very specific type of photo manipulation. I’ve got the basic UI built, and I’ve hooked into the camera framework, so you can either take a new photo, or use one you already took. I am displaying the photo in the main window, currently scaled to fit, but I will eventually add zoom and pan. I have the code written that allows the user to define a region of the photo to work on, by drawing a rectangle or ellipse. I draw the shape using Quartz 2D, which results in a red rectangle or ellipe drawn on top of the photo. So far, so good.

This is where I’m stuck. I need to do “something” to the bits in the photo in the region defined by the rectangle or ellipse and, at some point, a freehand shape. (Obviously I can’t reveal what the “something” is.) So, I have looked through the Quartz 2D docs and am trying to figure out how I can

  1. get the bits in the region defined by the user’s drawing
  2. swizzle the bits with my secret sauce to produce the desired effect
  3. get the swizzled bits back into the photo for display to the user

And here I sit. I have never done anything with graphics before, so this is all completely foreign to me. I can’t see how to do any of those three steps.

The next question, then, is should I be using OpenGL ES instead of Quartz 2D? The iPhone dev book I have taught a little bit of both, and the OpenGL stuff looked far more complicated than Quartz, which seemed like overkill for my situation. I don’t know.

I think I can accomplish what I want by creating an image mask, applying that to my original image, and then displaying the new image, but the mask creation function, CGImageMaskCreate, has me confused. I get most of the parameters, but I don’t understand the CGDataProviderRef parameter. Can anyone offer any sort of pointers to get me moving again? Are there any really in-depth Quartz 2D tutorials? The Apple docs on Quartz that I’ve read are very basic, and don’t really give examples.

Thanks for any help or pointers. I know I haven’t given you much to work with.

When Does an HQL Typo *Not* Cause A Parse Error?

I found something interesting at work yesterday. One of our developers mentioned that when he called a certain method with various sets of parameters, he wasn’t getting back what he expected to get back, based on what he knew was in the database. I put on my sleuth hat and began my investigation.

We use Hibernate for our database layer, and we prefer to use the @NamedQuery annotation to store our queries with the entities they represent. This works out very well for us. But back to the problem. I quickly got to the appropriate .java file and inspected the query. (Obviously I’ve changed the class names, but this is essentially what I found in the file.)

select distinct f
from Foo f
left join fetch
left join fetch f.baz
lef join fetch f.plonk
where = :fooId

Now, do you notice the typo? Check out line five. It says “lef” instead of “left.” When I first saw this, I thought to myself, “How can this even get parsed by Hibernate into SQL, let alone return any results.” We all work in a large room together, so I mused out loud about this problem. One of our other Really Smart Guys™ came over to have a look. He saw the typo, thought for a second and said, “‘lef’ is being treated as an alias for f.baz on the previous line.” And sure enough, he was right. Just as on the second line where you see “from Foo f,” that ‘f’ is an alias for the entity called Foo. We could have put aliases on each “left join” line, at each line’s end, had we wanted or needed to. By misspelling “left” on line five as “lef,” we unintentionally slapped an alias on the join that is on line four. Even though the query is split across multiple lines here, the HQL parser would see it as one continuous string, and after passing by “fetch f.baz” the next token it would see would be “lef,” which it would interpret as an alias for “f.baz.”

So, as far as parsing the query and translating it into SQL goes, everything is just fine. But there is still a problem caused by the misspelling. Since the parser decided that “lef” was actually an alias, the next bit that it sees is “join fetch f.plonk” which results in a regular inner join, instead of the outer join we really wanted. What this means is that for records in the Foo table who can’t be joined to records in the Plonk table, either because the key is null, or there just isn’t a record in the Plonk table that matches, those records will be excluded from the result set. That’s the behavior our developer was seeing. Changing “lef” to “left” made the whole thing work and the developer got the results he needed.

I’m Digging Java Again

I first started doing Java back in 1995. That’s quite a long time ago. Once I got going, I wrote Java code every single day, for thirteen years. I co-authored a Java book, gave talks on Java and was an all-around, Java Guy™. And sometime around 2006, I got bored with it. Completely and totally bored. I was a one-man shop at a small company, my code was running just-fine-thanks-very-much, and I didn’t feel like doing anything new with it, at all. I was more interested in Ruby and, to a lesser degree, Rails, so Java changes didn’t really interest me. And thus, I failed to notice some really cool stuff that was going on in Java-land.

In June of this year I joined a new company that is doing some rather advanced Java work. I had to get current, tout de suite, and in so doing, I’ve really gotten interested and engaged again. Spring and Hibernate have really changed from the older versions I was using, and so has JUnit. All for the better, from what I can tell.

And with this renewed interest, I’ve bought my first new Java books in over 3 years. I bought Effective Java (2nd Edition) to replace my first edition and Java Concurrency in Practice, because I heard good things about it. So far, I’ve read about 2/3 of  Effective Java. I used to buy Java books all the time. I have tons of them. But when I got bored, I stopped shelling out the cash on the Java books.

Java-land is still a very nice place to play. Sometimes you have to get an outside perspective to realize that.

Strange Seg Fault In Simple Cocoa App

I just got Aaron Hillegass’ 3rd edition of Cocoa(R) Programming for Mac(R) OS X (3rd Edition) and am working through it as if I never went through the 2nd edition. (It’s been so long since I did any Cocoa, that seemed like the smart thing to do.) Anyway, I’m in Chapter 4 on memory management and things were going fine. Until all of a sudden, the program started dying with a segmentation fault. After trying to figure out what I’d done wrong, I figured I’d mistyped something and just wasn’t seeing it, so I reverted my changes back to what I had in my Subversion repo. I then started making the changes again. And again the program died with a segfault. 

Any time I’m working through a programming book, I always type all the examples in by hand, rather than downloading pre-written code. I learn better that way. But in cases like this, I don’t have a problem with looking at the author-provided code to see where I went wrong. I downloaded the solutions from Aaron’s site and started comparing. I found the problem pretty quickly.

Originally, the code for creating the NSCalendarDate object for today’s date was

NSCalendarDate *now = [[NSCalendarDate alloc] init];

which, when you later add the code to release it, would have worked fine. But at some point between when I wrote that line of code and when I added the release, Aaron mentioned a convenience method on NSCalendarDate for getting back an autoreleased date object. That code looks like this

NSCalendarDate *now = [NSCalendarDate calendarDate];

I had changed my code to use that instead of the original version. That was in Chapter 3. So, when I hit Chapter 4 and added the call to release the date object, I ended up with my segfault. The chapter assumed that you still had the alloc & init calls, and so made no mention of the calamity that would ensue if you had switched to the other way of getting today’s date.

What made this difficult to find was when the segfault occurred. It didn’t happen when I called release on the date object. It happened on this line

[pool drain];

which is one line before the program ends. That’s boilerplate code there, so it was really strange that it was barfing. The reason this caused the problem is that this way of getting a date is autoreleased, which means that unless I retain it somewhere, it’s going to get deallocated when the NSAutoReleasePool is deallocated. But when I called release on it, I ended up setting it’s retain count to 0, so when the pool was drained, it ended up sending a release message to an object that had already been deallocated, which is a no-no.

Bottom line is that had this been a big program, this could have been really hard to track down. Of course, turning on the Objective-C 2.0 garbage collector would have solved the problem too, but then you have a Leopard-only program.

I Paid My $99 To Apple

As I said yesterday, I finally got my acceptance letter from Apple that essentially allows me to pay them $99 in order to develop iPhone software. I have just now paid the $99, so now it’s just a matter of waiting for the license or whatever it is that they will send out. Let’s hope it doesn’t take as long as it did for the acceptance letter.

As I write this, I am downloading the iPhone SDK. I played around with the first beta Apple released, but I haven’t really done anything with it since then. That was before they got Interface Builder going, so it should be a lot better now than it was then. I’m looking forward to getting started; now I just need to think of something that needs writing.

07/13/2008 00:37 Update: About 20 minutes after posting this, my Activation Code email arrived from Apple. Fun times ahead.

So *Now* Apple Will Let Me In…

After months of waiting, I received this email from Apple just a few minutes ago:

Thank you for applying to the iPhone Developer Program.

We have reviewed the information you submitted when you initiated your program enrollment request and we are ready to instruct you on the steps required to complete the enrollment process.

Please click here to review and agree to the Standard License Agreement. You must execute this agreement prior to completing the purchase and activation process for the iPhone Developer Program.

Once you complete and activate your purchase, your enrollment will be complete.

Thank you,

iPhone Developer Program

I guess now that the App Store is live, they can let more people into the program. Now I guess I’ll have to re-download the SDK and pony up.