Today I wrote a useful Objective-C method which uses Quartz 2D from the iPhone SDK to dynamically generate square thumbnails from any portfolio or landscape UIImage. I failed to find helpful results on Google for the various CGContextClipToRect/CGContextTranslateCTM/CGContextScaleCTM Quartz functions so after a lot of trial and error I managed to get the results I was after with a fairly efficient script – shown below.
So, if for example you want to get a 32×32 pixel square thumbnail, you can pass 32 as the ‘length’ parameter in the function below. For portfolio images the code will take a full square from the original image from the centre using the full width of your original portfolio image. Likewise, for a landscape image the script will use all the height available, clipping the right and left sides.
I think the most import thing I learned when writing this script is that when you call UIGraphicsBeginImageContext(CGSize) make sure that the size you pass in is the size you want your final image to be. Otherwise you end up having to call UIGraphicsEndImageContext() and UIGraphicsBeginImageContext() multiple times with different CGSize instances which I’m sure isn’t an optimised way to achieve the same results.
- (UIImage *)thumbWithSideOfLength:(float)length {
NSString *subdir = @”my/images/directory”;
NSString *filename = @”myOriginalImage.png”;
NSString *fullPathToThumbImage = [subdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%dx%d%@",(int) length, (int) length,filename];
NSString *fullPathToMainImage = [subdir stringByAppendingPathComponent:filename];UIImage *thumbnail;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:fullPathToThumbImage] == YES) {
thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];}
else {
//couldn’t find a previously created thumb image so create one first…
UIImage *mainImage = [UIImage imageWithContentsOfFile:fullPathToMainImage];UIImageView *mainImageView = [[UIImageView alloc] initWithImage:mainImage];
BOOL widthGreaterThanHeight = (mainImage.size.width > mainImage.size.height);
float sideFull = (widthGreaterThanHeight) ? mainImage.size.height : mainImage.size.width;CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);
//creating a square context the size of the final image which we will then
// manipulate and transform before drawing in the original image
UIGraphicsBeginImageContext(CGSizeMake(length, length));
CGContextRef currentContext = UIGraphicsGetCurrentContext();CGContextClipToRect( currentContext, clippedRect);
CGFloat scaleFactor = length/sideFull;
if (widthGreaterThanHeight) {
//a landscape image – make context shift the original image to the left when drawn into the context
CGContextTranslateCTM(currentContext, -((mainImage.size.width – sideFull) / 2) * scaleFactor, 0);}
else {
//a portfolio image – make context shift the original image upwards when drawn into the context
CGContextTranslateCTM(currentContext, 0, -((mainImage.size.height – sideFull) / 2) * scaleFactor);}
//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);[mainImageView.layer renderInContext:currentContext];
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(thumbnail);[imageData writeToFile:fullPathToThumbImage atomically:YES];
thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];
}
return thumbnail;
}
So if, for example your original image looked like this:

Using the method above you could request a 100×100 pixel thumbnail which would result in this:
![]()
I’ve been lucky enough to get some Research & Development work in over the last few weeks. The latest batch of work has included some experimental stuff with the the new Microphone features introduced in Flash Player 10.1 which is currently in beta. You can download the required Flash Player here.
In the example below the app will automatically record your voice until you stop talking and will playback the sound bytes as Barry White or a Chipmunk (adjust using the slider).
Prior to Flash 10.1 you could manipulate the sound bytearray from an mp3 at runtime but you couldn’t access the native incoming microphone data. I can see this modification to the Flash Player leading to some really cool future games and apps.
In my example there’s currently no encoding to mp3. I just save the incoming sound byte arrays in memory. I think there may be as3 mp3 encoders already written if this was a project requirement.
I’m also really intrigued by Didier Brun’s R&D work on voice recognition in Flash Player 10.1:
http://www.bytearray.org/?p=1151
Looks like he’s really getting somewhere from those example videos. I guess one approach might be to render the sound data samples to bitmaps and run the sort of blend, colour threshold comparisons that you achieve with Flash WebCam motion detection.
Thanks to Andre Michelle’s shared work on pitching MP3s and Devon Wolfgang’s microphone code example I was able to get the above up and running.
Here’s the source
Prodigi is a Flex 4 application I developed for Mangahigh.com, – a powerful games-based math elearning system that features more than 12,000 questions with hints and worked solutions. Prodigi is adaptive, and increases question difficulty based on student performance. Take a look at the promotional video MH recently released on YouTube.com:
Behind the scenes Prodigi also deals with the question authoring and auditing workflow and has enabled Manga High’s teachers to rapidly develop an extensive database of challenging Maths GCSE questions that students all over the UK (and soon the US) can use for revision and GCSE learning.
There’s a lot of buzz in the Flash community today following Adobe’s big announcement at Max Keynote regarding their new up and coming Flash CS5 publish to iPhone feature.
In future versions of Flash, Flash Builder and ActionScript 3 you will be able to write Flash Applications that you can deploy to the iPhone as native iPhone Apps via a new Flash to iPhone compiler that the Adobe engineers have written.
Flash Developers will still need to apply/register and pay to become Apple iPhone Developers like all the other 1000s of iPhone Devs. They will also need to go through the same slow submission process.
What’s important to clarify is that NO, we still don’t have Flash Player on the iPhone. So websites containing Flash content will still not display in Safari on iPhone. This is really the missing feature that the iPhone would benefit from. This is the feature that Apple ultimately hold the key to as they build and own Safari, the default native web browser on iPhone. One alternative Adobe may have considered would have been to develop their own web browser iPhone App (including Flash Player) as earlier this year Apple has approved a bunch of web browser apps that compete with Safari and are available now in the App Store.
So, Apple won’t play ball with Adobe and won’t permit the Flash Player Plugin on iPhone. The main reason for this is that Flash Player is a memory hog and Flash Player in Safari would probably just lead to endless Safari crashes.
Contrary to this, however, in other Adobe news today Flash Player 10.1 was released. 10.1 is a major Flash Player upgrade and the biggest changes appear to be all the support added for Mobile Platforms (apart from the iPhone). Kevin Lynch, Adobe CTO, highlighted the major performance improvements introduced in the latest release of the Flash Player. He displayed test result figures that showed a number of popular Flash and Flex Applications running 100%+ more efficiently in Flash Player 10.1. This, without any changes to the Flash Applications themselves. Perhaps Adobe took note of Joa Ebert’s Leaving the Sandbox presentation at Flash on the Beach this year when he criticized the Adobe compiler and it’s optimization flaws.
With Adobe making big optimization changes to the Flash Player (and the compiler?) these are welcomed enhancements from a Flash Developer’s perspective. AS3 was such an improvement from an OOP coder’s standpoint but garbage collection was (and still is) pretty lousy. It’s very easy to create bloated SWFs – much easier than it ever was with AS2 or AS1. I guess that following all the new Flash Player features Adobe have introduced over the last 3 or 4 years the optimization fell behind on the priority list. My guess is that with Apple not permitting Flash Player on the iPhone this has helped to encourage Adobe to add these new optimizations to FP 10.1 and merge mobile and desktop versions of the Flash Player in this latest version of the player. I reckon Flash Player in Safari is not too far off with these improvements. Fingers crossed…
So, I’m a Flash / Flex Developer and have written native Objective-C applications for the iPhone. I’ve also presented my experiences of iPhone Development from a Flash Developer’s perspective at the Flash on the Beach ‘09 Elevator Pitch. Will I now move to developing iPhone Apps using Flash? Well, probably not as I like the UI components that are included in the iPhone SDK as do the majority of the millions of exisiting Phone owers. Yep, that’s right – you develop iPhone Apps in Flash and you ain’t gonna get access to all the existing iPhone SDK components built by Apple. It’s the same problem as you may have experienced if you try to develop an iPhone App using existing technologies that compile to iPhone friendly binaries such as Unity 3D.
Personally, I just wish Flash Developers would get a bit more adventurous and try other programming languages rather then nagging Adobe to play middle man. Please Adobe, compile my dodgy ActionScript into Objective-C!
Objective-C is really not that hard folks, go on and take the plunge!
Thanks to Yezzer and Matt I managed to get hold of a video of my 3 minute “Elevator Pitch” at Flash on the Beach last week. Here it is!
The point I was trying to get across in my 3 minutes was that the differences between OOP programming languages such as ActionScript 3 and Objective-C really just boil down to syntax variants and not too much else really (memory handling aside which is a nightmare on the iPhone!). Flash runs in the browser (or desktop or mobile etc etc), Objective-C runs on the iPhone. Both languages contain classes, methods, static methods, constants, components etc etc.
The iPhone SDK has an MVC framework built in. MVC is a programmatic design pattern, it’s not unique to the iPhone and it’s not unique to PureMVC, Cairngorm or the other ActionScript Frameworks that Flash/Flex Developers often now use to build Apps.
ActionScript has matured so much since I started out as a Flash Dev 10 years ago. With it’s similarities to Java and other languages that have traditionally been considered more *hardcore* good OOP AS3 ActionScripters should no longer see themselves as just developers of Flash content – we now have skills that enable us to jump into other OOP languages quickly and easily (well not always!).
From a personal perspective I welcome Flex Builder and kiss goodbye to the timeline!
I guess the subject matter of my presentation – ActionScript to Objective-C (and everything in between!) was never going to appeal to all the 1000+ attendees of Flash on the Beach but it sounds like my point was taken by a few at least
