Dec 282012
 

Not my code, but too awesome not to share. See carlj’s https://gist.github.com/3782351

+ (UIImage*)imageNamedForDevice:(NSString*)name {
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if (([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale) >= 1136.0f) {
            //Check if is there a path extension or not
            if (name.pathExtension.length) {
                name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension]
                                                       withString: [NSString stringWithFormat:@"-568h@2x.%@", name.pathExtension ] ];
            } else {
                name = [name stringByAppendingString:@"-568h@2x"];
            }
            
            //load the image e.g from disk or cache
            UIImage *image = [UIImage imageNamed: name ];
            if (image) {
                //strange Bug in iOS, the image name have a "@2x" but the scale isn't 2.0f
                return [UIImage imageWithCGImage: image.CGImage scale:2.0f orientation:image.imageOrientation];
            }
        }
    }
    return [UIImage imageNamed: name ];
}

What does it do?

Apple lets you have a Default image name Default-568@2x for the newer 4″ tall iPhones. What if your app needs a specific graphic, say a background image, while running? Easy!

[UIImage imageNamedForDevice:@"background.png"]

And just make sure you have background.png, background@2x.png and background-568@2x.png in your app and you’re good to go!

This has been incorporated into the open source EnkiUtils.

Dec 102012
 

The app I’m working on does not use UIStoryboards, but the previous developer had used ButtonPeoplePicker and when I went to rev BPP I found that it now used UIStoryboards. Hmmm, how can I integrate BPP into my app?

It was easy. First, you load your storyboard:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];

This gives you a Storyboard object, note the name that ButtonPicker used. That could collide with other names so you should change it to something like BPP_MainStoryboard_iPhone. Then to bring up the BPP all you have to do is:

ButtonPeoplePicker *svc = [storyboard instantiateViewControllerWithIdentifier:@"ButtonPeoplePicker"];

Where the ButtonPeoplePicker name is the “storyboard id” that I assigned with IB. (It was nil in the code I got, presumably because it wasn’t used.) Then you just do the normal:

self presentViewController:svc animated:YES completion:nil];