views:

129

answers:

1

For a concept I'm developing, I need to load XIB files manually and by using class and instance method swizzling I have been able to intercept calls to imageCustomNamed, imageCustomWithContentsOfFile and imageCustomWithCGImage for the UIImage class and initCustomWithImage for UIImageView. What I want to to is detect the image name and replace it with some content rendered on the fly in place of the one set in IB at design time.

for example:

when XIB has an UIImageView object whose Image property is set to "About.png", I need to intercept the loading of that image and replace with another one depending on certain condition. It would be ok even to replace the image after the UIImageView object has loaded the image set at design time, but looks like the original name of the UIImage used to set the content of UIImageView is not stored anywhere.

I cannot use IBOutlets as I don't know the content of XIB file before hand; this is a class that should work on any XIB not just a particular one.

The custom methods are in fact being called in placed of the default ones, but looks like that when loading XIb the system uses imageCustomWithCGImage which accept a CGImageRef as argument; from it there is no way to know the origin (i.e: the image file name)

Any idea on how I can intercept the loading of images?

A: 

In OS 3, at least, you can override UIImageNibPlaceholder's initWithCoder:. Replace it with something like this:

-(id)hack_UIImageNibPlaceholder_initWithCoder:(NSCoder*)coder
{
  NSString * name = [coder decodeObjectForKey:@"UIResourceName"];
  [self release];
  return [[UIImage imageNamed:name] retain];
}

I'm not sure what happens if you load nibs from other bundles (e.g. frameworks).

tc.
I thought about it, but it is part of the private stuff, so it's a no no
It's all "private stuff". There's no guarantee that you can replace +[UIImage imageNamed:] and expect things to still work across an OS update. Sometimes it's the only way.
tc.