Tracking Image Loading.

I recently had a hunch that Samples was loading too many images at app launch. I had to figure out how to get a list of what images are loaded, no matter if the source is a Storyboard or a call to +[UIImage imageNamed:].

Symbolic breakpoint configuration in Xcode

Here’s how I did it, step by step:

  1. Bring up the Breakpoint Navigator (⌘7)
  2. Click the little + button in the bottom left corner
  3. Select Add Symbolic Breakpoint…
  4. Set up your breakpoint as shown above:
    • Type imageNamed: in the Symbol field
    • Click Add Action, select Debugger Command
    • In the command field, type po $r2
    • Check Automatically continue after evaluating
  5. Run on device (⌘R)

Now all the names of images that are about to be loaded will be logged to the Debug Area. For added fanciness, you can select Debugger Output there, so your trace won’t be polluted by other log messages coming from your target.

For the curious: po $r2 will print the Cocoa object pointed by the r2 register, which is where the first argument of an Objective-C message is stored - in our case, the name of the image to load. The value for self is stored in r0, and the value for _cmd (the selector to invoke) is stored in r1. Check out the documentation for objc_msgSend and friends to learn more about the Objective-C runtime.

If there’s an easier way to accomplish this, let me know!