Monday, May 17, 2010

Some Good Advice

In the comments of my previous post, Joe Conway, one of the fine instructors over at the Big Nerd Ranch, co-author of one of the newer books on the iPhone SDK, and all-around nice guy had some really good suggestions. One of his comments was (I'm paraphrasing) "don't use an image when the image can be easily drawn with Core Graphics.

Joe posted some code to replace the gradient background in the previous post. Admonished for my laziness and inspired to make the code better, I incorporated his suggestion. I then finally went and bought Opacity (a vector drawing program that will output Core Graphics code) and used it to create a subclass of UIButton that will draw itself in a style similar to the buttons in UIAlertView using only Core Graphics calls, no images. These buttons can be drawn at any size and will draw correctly without the need for any images.

Once I've mastered Opacity, I'd like to replace the alert background image with Core Graphics code. Joe's also got some suggestions in his comments about how to make the status bar gray out, which I may incorporate into a future version.

If you're curious, you can check out the revised project incorporating Joe's suggestion as well as the imageless version of the alert button.

If you do any serious amount of iPhone work, you really should check out Opacity. If you're a hobbyist, they have a less-expensive version that may suit your needs.

Thanks Joe!



11 comments:

Ken Pespisa said...

Coming from a .NET background, I occasionally feel lost in the world of iPhone development. (I'm just a caveman, your world frightens and confuses me.) I had one of those moments while reading your post.

I understand using Core Graphics instead of images is more efficient, but is that something I really need to worry about on a regular basis? It seems like a step in reverse for all but a few rare cases.

Even if it is only a few lines of code (Joe's simple gradient example was 13 lines - more than a few, really), that's a lot of time when you have a dozen or so images. And since we're talking about small images anyway, there's not much overhead to worry about.

It is a stretch to say you're lazy for not using code to generate your images. It's a cool idea, but I can't spending the time on it unless it was crucial to performance.

Jeff LaMarche said...

Ken:

I made a similar point to Joe in his original comment. For many situations it doesn't make sense. However, when you're talking about generic objects that will be used a lot, it really does make sense.

In the example of a gradient button, for example, if you have a large application with 100 buttons, the difference becomes pronounced.

If it's something that's used in a single place in the application, then you're probably wasting time and effort trying to use CG.

It's good to be aware of Core Graphics' capabilities, but there's no hard and fast rule about when you should use it. Certainly not always.

warmi said...

"I understand using Core Graphics instead of images is more efficient,"

Actually, it is less efficient in terms of pure CPU utilization but it is more convenient and reusable.

Andr said...

This tutorial shows how to create a custom UIView class for your iPhone Application. Opactiy 1.4 allows the developer to visual layout a view and generate cocoa touch source code from it.

http://www.youtube.com/user/SpicyApps4iPhone

Mostly Torn said...

Thanks for the tip about Opacity generating Core Graphics code. I've been using Opacity for a few months and had no idea it had that feature.

Time to go back an read the help file to see what other goodies I missed.

Joe Conway said...

"Actually, it is less efficient in terms of pure CPU utilization but it is more convenient and reusable."

If speed is a problem - which it shouldn't be - you can certainly cache Core Graphics commands into an image in RAM using the UIGraphics suite of functions. But, given that interface elements are relatively rarely redrawn, I typically just render on the fly. Also, if you are doing a Universal (iPad/iPhone) application, you don't have to bundle two images for the differing resolutions. I think that the resolution independence is the biggest advantage.

But, Ken, you are right, Core Graphics is a fringe skill. You can totally get away with using images everywhere. Personally, I come from a graphics background and it is much more simple for me to generate an image with Core Graphics than it is to get an artist to draw one up. As time goes on, you will end up building a pretty nice library of Core Graphics routines that make your life easier (and cheaper, since artists are expensive!).

Also, I will say, working in the iPhone contracting world, relying on an artist (especially one hired by the contractee) is usually misery. My tendency to use Core Graphics has been fostered by many an artist email that says something to the effect of, "Here is the image you requested that will appear as a square 40x40 button in the application. It is it in the newest Photoshop file format version with 15 layers at a non-square resolution of 7000x10000 in case you wanted to put it on a billboard." :)

Sayle said...

You mentioned a great utility (Opacity) in this entry. I learned about Accessorizer from your blog as well which has saved me hours of time already.

Myself like many developers came into iPhone development from another platform like .NET or Java so we don't know all these great apps like Opacity to use for development.

Would it be possible for you to write an entry listing some of these great utilities you use for your development work?

Thanks and keep up the great work!

SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels

Hire iphone developer said...

Hi,
The programmer developer you are checking out should be able to work for you on the part time as well as full time basis, like on hourly, weekly or monthly basis, which would be as per your business requirements.
Thanks.


hire iphone developer

healthpharmacyrx1 said...

viagra soft Online Rezeptfrei
achat kamagra france
achat cipro en ligne
cialis super active plus Prix de Vente
Achat viagra Sans Ordonnance
flagyl Ohne Rezept
diflucan Rezeptfrei Kaufen
lasix Ohne Rezept
lovegra Rezeptfrei Kaufen
lamisil Rezeptfrei
Order amoxil
kamagra Rezeptfrei
Ohne Rezept clomid
accutane Rezeptfrei Kaufen