Search code examples
objective-ccocoadebuggingnsimage

Inspect an NSImage reference while debugging?


Is there a feature in Xcode that allow you to inspect NSImage references while debugging?

Similar to how webkit inspector let you inspect image references in webpages.

Failing that, what's the easiest way to debug while working with NSIMages?

webkit inspector


Solution

  • UPDATE: with Xcode 5 and newer take a look at the answer by MagerValp. it looks like you can preview a image right in Xcode.


    No, as far as i know there is no such feature in Xcode. If your application creates or manipulates images and you would like to see the visual representation of the data behind a reference to NSImage, you could dump the image to the filesystem.

    Just create a category, which adds -(void)dump to NSImage like this:

    @interface NSImage (Dump)
    - (void)dump;
    @end
    
    @implementation NSImage (Dump)
    - (void)dump {
        NSBitmapImageRep *imageRep = [[self representations] objectAtIndex: 0];
        NSData *data = [imageRep representationUsingType: NSPNGFileType properties: nil];
        [data writeToFile: @"/tmp/image.png" atomically: NO];
    }
    
    @end
    

    Now you can set a breakpoint somewhere in your code and call the method dump on your reference of NSImage by typing following in the command line of the debugger:

    po [image dump]
    

    the only change in your source code is the import for the category.