Clicky

iOS Dev Nugget 114 Preview an Image for Debugging

.

Need to run a code review on your codebase? Hire me

Here's a simple, yet handy piece of code that has helped me many times over the years. It allows you to display an image on the iOS screen for debugging purposes. For example, if I let user pick a photo from their photo library and do some processing with it, during development, I might want to see if the result is correct, I can just run:

UIImage* img;// Image after processing
[img showGlobally];

And the image will be displayed. Tap on it to hide it. While newer versions of Xcode has the ability to inspect images in the debugger, this trick is still invaluable when you are on the device. e.g. you could hook it up to shake to display the image just processed, as part of your debugging toolset.

Here's the implementation:

@interface UIImage(UIImage_debug)
- (void)showGlobally;
@end

@implementation UIImage(UIImage_debug)
- (void)showGlobally {
    UIView* parent = [[[UIApplication sharedApplication] delegate] window];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:self forState:UIControlStateNormal];
    btn.frame = parent.bounds;
    [btn addTarget:btn action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor blackColor];
    btn.center = CGPointMake(parent.frame.size.width/2, parent.frame.size.height/2);
    [parent addSubview:btn];
}
@end


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues