So i was going to post about something completely different today, but as i was typing i looked at my code, and thought it was verbose and a little bit ugly.
So i made it awesome, and as everyone knows to make your code awesome you add some blocks to it.
My problem was simple, i was faced with the need to create two CGBitmapContext, for the uniformed the following code is required to prepare a bitmap context for drawing.
So after a bit of thought, i decided what i really wanted was a method that did all of this for me, and meant that i didn't have to worry about constantly checking the code for leaks (DRY), and that was pleasant to look at and i came up with the above. The block that you pass in is given a fully formed CGBitmapContext, and the method returns a UIImage generated from that context. Almost like a UIView/CALayer.
So now your thinking, "Yeah, thats cool, but why the [INSERT FOUR LETTER WORD] would i want to use it ?" Well young grasshopper, have you ever wanted to mask a image in code? You know to do those trendy rounded corners ... well yes you can use CALayer's however the idea of using those off the main thread makes me uneasy, and we all know the cool kids do things in the background.
The above actually creates a rounded rect on the fly, and masks the image with it. It's made to be used in a category on UIImage. But look closer, yep thats right kids, no boiler plate, ZERO, NADA, 另, SQUAT (i think you get the point)!
I should take a moment to mention that the awesome code for the rounded rect comes from the awesome Oliver Drobnik, i have the utmost respect for this guy, not just for this snippet, but if you've ever seen his Rich text label and it's associated projects, you'll understand why real soon.
The musing and sometimes not so wise words of Jonathan Dalrymple, Global Traveller, Programmer, Financial Rocket Scientist, Conspiracy Theorist, Part-time comedian, full-time funny man and whatever else i randomly decide to do.
Showing posts with label mobile development. Show all posts
Showing posts with label mobile development. Show all posts
Wednesday, September 07, 2011
Tuesday, January 04, 2011
NSNotificationQueue, Coalescing and SIGABRT
So it's one of those days, your steaming along adding a feature and then bam, you hit a road block. Well here was mine.
I've come to love many cocoa API's over the last year. One i've been late to embrace is NSNotificationQueue. What can i say that hasn't already been made clear ... it's a queue for notification objects ... well almost it has one particular feature that makes it totally awesome, Coalescing.
Coalescing is useful for when you want to call a method multiple times but only have it execute it's action once every so often. Since i've started coding for iOS i've been a fan of simply calling [UITableView reloadData] to refresh my table views, it's simple concise and i don't have to worry about index paths etc. However if you are drawing complex cells, and you need to call this method while you process some data you really don't want to happen every single time, the user isn't playing COD, they just want to see their data, and not have it flick around. So in my UITableViewDataSource classes i do this ...
With the handler to the reloadNotification calling the actual reload method. This means that in one area of my app, where i am listening for NSManagedObjectContextObjectsDidChangeNotification (gotta love cocoa conciseness )the reload method gets called just once instead of 10+ times.
Awesome right ....
Well there is a problem, you might have noticed that enqueueNotification: takes a postingStyle. This calculates when in the duration of the run loop the queue will attempt to deliver your notification. Because i want my Run loop to be a free spirit and not care about what my crazy background threads were doing i would usually choose NSPostWhenIdle. Well i did till today.
The problem is the UITableView that this data source is assigned to can be dealloc'd at anytime, and while my dataSource is a good cocoa citizen and removes it's self as an observer before it is dealloc'd the notification has already been assigned it's target and is waiting till the run loop is idle ... read waiting till after my object has been dealloc'd. This of course means the the objective-c runtime attempts to call the reload method on dealloc'd object, and we all know what happens when that happens ... Well i thought i did. I code with NSZombieEnabled on, so in such a situation i expect to see a nice friendly message saying i've sent a message to an invalid object ... I got that message ... sometimes. Instead 9/10 times i got a big, fat, bold SIGABRT.
Oh, but you have stack traces ... um no. When crap happens in the objective-c runtime, you close your eyes and pray that they will go away, well thats what i do.
Anyways the solution was to ensure that notification was dispatched as quickly as possible, so NSPostNow was a much better fit. My professional($_$) opinion is that unless you have a long running object (ie the app delegate) or a singleton, never use the NSPostWhenIdle.
Conclusion
NSPostNow > NSPostWhenIdle
(9 out of 10 times)
I've come to love many cocoa API's over the last year. One i've been late to embrace is NSNotificationQueue. What can i say that hasn't already been made clear ... it's a queue for notification objects ... well almost it has one particular feature that makes it totally awesome, Coalescing.
Coalescing is useful for when you want to call a method multiple times but only have it execute it's action once every so often. Since i've started coding for iOS i've been a fan of simply calling [UITableView reloadData] to refresh my table views, it's simple concise and i don't have to worry about index paths etc. However if you are drawing complex cells, and you need to call this method while you process some data you really don't want to happen every single time, the user isn't playing COD, they just want to see their data, and not have it flick around. So in my UITableViewDataSource classes i do this ...
With the handler to the reloadNotification calling the actual reload method. This means that in one area of my app, where i am listening for NSManagedObjectContextObjectsDidChangeNotification (gotta love cocoa conciseness )the reload method gets called just once instead of 10+ times.
Awesome right ....
Well there is a problem, you might have noticed that enqueueNotification: takes a postingStyle. This calculates when in the duration of the run loop the queue will attempt to deliver your notification. Because i want my Run loop to be a free spirit and not care about what my crazy background threads were doing i would usually choose NSPostWhenIdle. Well i did till today.
The problem is the UITableView that this data source is assigned to can be dealloc'd at anytime, and while my dataSource is a good cocoa citizen and removes it's self as an observer before it is dealloc'd the notification has already been assigned it's target and is waiting till the run loop is idle ... read waiting till after my object has been dealloc'd. This of course means the the objective-c runtime attempts to call the reload method on dealloc'd object, and we all know what happens when that happens ... Well i thought i did. I code with NSZombieEnabled on, so in such a situation i expect to see a nice friendly message saying i've sent a message to an invalid object ... I got that message ... sometimes. Instead 9/10 times i got a big, fat, bold SIGABRT.
Oh, but you have stack traces ... um no. When crap happens in the objective-c runtime, you close your eyes and pray that they will go away, well thats what i do.
Anyways the solution was to ensure that notification was dispatched as quickly as possible, so NSPostNow was a much better fit. My professional($_$) opinion is that unless you have a long running object (ie the app delegate) or a singleton, never use the NSPostWhenIdle.
Conclusion
NSPostNow > NSPostWhenIdle
(9 out of 10 times)
Thursday, December 16, 2010
Offscreen drawing on the iPhone using NSOperations, CGImage, CGLayer
What?
Creating and or modifying images offscreen or in a background thread.
Why?
Chiefly for performance. The main thread of an iPhone application is usually fairly busy doing all sorts of things. By rendering complex images in the background, you can do all manor of things. In my case I wanted to know how to render an image larger than the size of the device screen, and then save that image to disk.
For my fellow lovers of NSOperationQueue(aka possibly the most awesome class in Cocoa), this allows you to bundle image processing and generation in to NSOperation subclasses or if you like the new hotness a block, and add it to the queue.
How?
Well with a lot of C. If you have ever overridden UIView's drawRect: then most of what is below should make perfect sense.
First thing is first, as we want to operate on our own thread we have to create everything ourselves. Primarily that means no quick calls to UIGraphicsGetCurrentContext().
The key thing to remember is that the drawing coordinates are inverted. This means that CGPoint(0.0f,0.0f) is actually the bottom left not the top left corner. I'm lead to believe this is a hold over from the Postscript drawing system that originated on the mac, and this is how the big boys do it. So stop whining and code.
Nicely it would appear that the UIImage representation methods automatically invert the image, so you only have to invert your coordinates for CGContext drawing calls.
Exactly How?
The key thing to remember is that the drawing coordinates are inverted. This means that CGPoint(0.0f,0.0f) is actually the bottom left not the top left corner. I'm lead to believe this is a hold over from the Postscript drawing system that originated on the mac, and this is how the big boys do it. So stop whining and code.
Nicely it would appear that the UIImage representation methods automatically invert the image, so you only have to invert your coordinates for CGContext drawing calls.
Exactly How?
This is the process
*Create a CGColorSpace
*Create a CGBitmapContext
*Create a new CGLayer
*Get the the CGLayer's context
Draw into the context as you would normally, using the CGContext methods
*Render that CGLayer into the CGBitmapContext
(if you want an image)
*Create a CGImage from the CGBitmapContext
*Convert the CGImage into a UIImage
*Use one of the UIImageJPEGRepresentation or UIImagePNGRepresentation methods to get an image that can be saved to disk or sent over the wire etc.
NOTE: This code will not work unless you have a image named sample.jpg in your bundle or change the assignment to the backgroundImg variable.
Sample code
Go forth and code.
Tuesday, March 17, 2009
So thats why Java ME is so popular
I have to hand in my EWT (Emerging Web Tech) assignment tomorrow.
Part of the assessment is a look at Development platforms in the mobile space. Until recently native was a dirty word as it usually involved device specific C code. Java has always been fairly popular, but i've always dismissed it for it's non native look and feel.
However, after exploring java ME some more i can see why it appeals to many developers.
The Hello world application is fairly simplistic compared to iPhone, Andriod and especially symbian equivalents.
But we all know that it doesn't hold a candle to the Palm Pre. I would demostrate how amazing the Palm Pre is, however i don't have one yet!
Now if i could just work out how to install the thing in OSX we might be on to a winner!
Lastly the 3 iteration of the iPhone SDK was released today. It's good to see apple can still pull some magic out of the hat. I call it magic, because only in the world of apple can cut and paste be termed as a feature on a smartphone in 2009. FYI i've been cutting and pasting on my E61 for 2 years!
Part of the assessment is a look at Development platforms in the mobile space. Until recently native was a dirty word as it usually involved device specific C code. Java has always been fairly popular, but i've always dismissed it for it's non native look and feel.
However, after exploring java ME some more i can see why it appeals to many developers.
The Hello world application is fairly simplistic compared to iPhone, Andriod and especially symbian equivalents.
But we all know that it doesn't hold a candle to the Palm Pre. I would demostrate how amazing the Palm Pre is, however i don't have one yet!
Now if i could just work out how to install the thing in OSX we might be on to a winner!
Lastly the 3 iteration of the iPhone SDK was released today. It's good to see apple can still pull some magic out of the hat. I call it magic, because only in the world of apple can cut and paste be termed as a feature on a smartphone in 2009. FYI i've been cutting and pasting on my E61 for 2 years!
Tuesday, November 13, 2007
Google's Android: My two cents
I love it. At least from a developers point of view. Now the real question is, can they get the devices with the right form factors to really complete with the big boys, MS, Symbian, and now Apple.
With HTC behind them, there is no reason why they can't. So why does a tech skeptic like me like this FOB (Fresh off the boat) platform so much. Well here's why
Quote of the Day
Some are more hungry than others...
With HTC behind them, there is no reason why they can't. So why does a tech skeptic like me like this FOB (Fresh off the boat) platform so much. Well here's why
- SDK for OS X, Linux & Windows (Take note Apple!)
- It uses Java as the language of choice.
- Each app runs in VM, they borrowed an idea from palm os(kinda)
- They provided a C Api
- XML GUI layouts, nice and open
- The Platform is wide open
If you wanna look at this from a web 2.0 mashup point of view, its like the following
Microsoft's level developer support + Googles Open-ness + Apples inovation
Alternatively Lets compare it to it's rivals
Symbian - 'Modern' widely supported Language (Java vs C++).
Microsoft - It doesn't look like it was retrofitted to be used in a phone
Palm - Multiple device makers, means better form factors + it's a brand new os
Apple - It's not a Objective-C only world, and locked down like a [insert word of choice]
Others - It's got traction, and the developer community behind it, we all love google.
I'm getting to work on developing some apps for this platform. As i said it's got my support, it's solid under the hood, and i look forward to queuing for my first device.
BTW i hate the iphone if anyone is wondering.
Quote of the Day
Red Light District
Some are more hungry than others...
Subscribe to:
Posts (Atom)