How does UIImage get constructed when created from an XIB?

Go To StackoverFlow.com

4

I'm trying to do some fanciness with XIBs and that includes wanting to somehow get and store the paths of images loaded from the xib. To do this, I made some categories and did some method swizzling to override all the UIImage constructors to save their path before calling their parent constructor.

But due to Apple's black box BS with all their XIB stuff, absolutely none of the exposed constructors for UIImage seem to get called when I create a UIImageView through [UIViewController initWithNib...].

Does anybody know what function call happens or how they do this? I can't find any information whatsoever that exposes what initWithNib actually does behind the scenes.

Thanks!

EDIT: If you're in a similar situation, you may try using the accessibilityLabel / accessibilityHint which is automatically populated with the image path. The only issue is that accessibility needs to be enabled or these values are nil.

2012-04-03 22:45
by Eli
Why not just subclass UIImage or UIImageView and set the objects in the XIB to be your new EliImage or EliImageView objects and then you can do all sorts of crazy things - Michael Dautermann 2012-04-03 22:48
I still can't get the image path from that, unfortunately - Eli 2012-04-03 23:33
@Eli if the images can be loaded from the xib, then they're in your bundle, right? I think that's the straighter line to your image paths - danh 2012-04-04 00:06
Well yes but then you don't know exactly which path is loaded for which image - Eli 2012-07-12 00:51


2

I know the objects constructed from a Nib are being unarchived according to the NSCoding protocol; you need to override initWithCoder: in this case.

You could use swizzling to replace UIImageView's initWithCoder: method, then snoop around to see if an image name or path is available in any of the coder's keys. This might be more effective than hacking UIImage itself, since for all we know UIImageView could be using a custom subclass that you don't have access to.

2012-04-03 22:53
by benzado
hi benzado! I'm trying to do right this, any idea how to get image name on initWithCoder?? thank - Frade 2015-03-24 12:19


1

the initWith... methods are meant for programatically creating UIImageView objects.

Sounds like you want to catch things as they are instantiated from XIB files. That would be the parent class UIView's [initWithCoder:] method.

As the UIView documentation says:

initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.

2012-04-03 22:51
by Michael Dautermann


1

You are both close to right - I tried doing this with UIImageView which works for initWithCoder as you guys are both suggesting. However, UIImage doesn't get initWithCoder called for some reason, instead it uses initWithCGImageStored:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation.

If and when I actually get the path out of this as I desire I'll post it up here. Thanks for the help, gents.

2012-04-03 23:01
by Eli
Did you ever get this figured out - Codezy 2013-12-10 05:54
I ended up diving into the XML, unfortuantely - Eli 2014-01-26 01:30