View Images And PDF Files In Realtime With Mac OS X Preview While Debugging iOS Apps

David Dengg recently contacted me with a unique tip that can be quite helpful if you are creating or manipulating UIImage objects and/or PDF files in your iOS app.

The basic idea is to show objects in realtime in the Preview app on your Mac development machine while your application is running in the simulator. Let me show you how this works – in the code below (from yesterday’s post on cropping an image), I create a UIImage object that is cropped from another image:

UIImage *image = [UIImage imageNamed:@"mountains.png"];
 
// Create rectangle from middle of current image
CGRect croprect = CGRectMake(image.size.width / 4, image.size.height / 4 ,
  (image.size.width / 2), (image.size.height / 2));
 
// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect);
 
// Create new cropped UIImage
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
 
CGImageRelease(imageRef);

You can preview the new cropped image by writing the UIImage data to a file and using the system command to open the file in the Preview app on the Mac:

if (croppedImage != nil)
{
  [UIImagePNGRepresentation(croppedImage) writeToFile: @"/tmp/croppedImage.png" atomically:YES];
  system("open /tmp/croppedImage.png");
}

This should also work equally as well if you are creating PDF files inside your application.

The one caveat here is that this will only work when running your app on the iOS simulator.

Thanks for the tip David. Check out David’s website or follow him on Twitter.

If you have an idea for an iOS Developer Tip or Trick, drop me a line.

Comments are closed.