3

problem:
framework will cache the image data when above:

[UIImage imageNamed:]

I don't want the caching happen,so I can replace it by

[UIImage imageOfContentOfFile:]

Seems solved,but after my tests,in xib's instantiate progress, framework uses imageNamed: rather than imageOfContentOfFile.
That is, images loaded by xib still cached.

So I try to override the UIImage's imageNamed: method, make all this method DO NOT CACHE.

--try1. category:

@implementation UIImage (ImageNamedNoCache)

+ (UIImage *)imageNamed:(NSString *)name; 
{
    NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    return [UIImage imageWithContentsOfFile:path];
}

@end

--result:warning."Category is implementing a method which will also be implemented by its primary class"

--try2. runtime replace the method's imp

    //get super method
Method method_imageNamed = class_getClassMethod([UIImage class], @selector(imageNamed:));

//get my method
Method method_myImageNamed = class_getClassMethod([self class], @selector(myImageNamed:));

//get my imp
IMP imp_myImageNamed = method_getImplementation(method_myImageNamed);

//set super method's imp to my imp
method_setImplementation(method_imageNamed, imp_myImageNamed);

--result these too try only work when I invoke [UIImage imageNamed:]
but xib's instantiation doesn't work.

help me, thanks

user1610768
  • 109
  • 7
  • What's wrong with the category? You can use clang directives to suppress the warning quite easily. Not that I'd recommend doing this, but I suppose you have a compelling reason to keep your images from being cached. – Carl Veazey Sep 27 '12 at 04:39

4 Answers4

1

Sounds like a job for method swizzling. I find it dangerous and unnecessary, but if you are so inclined, it is an option. Use at your own risk.

eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
0

The category method should be fine, just give your method a different name. It's my understanding that it's not good practice to override methods in a category, but adding a new one is fine.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Problem is override the method that load image during xib's instantiation.If only I use it,Certainly I can create another method instead of overriding it. – user1610768 Sep 27 '12 at 08:04
0

Correct me if I do not interpret correctly, but it looks like your core problem is that you are trying to get framework (i.e., Apple code) to call imageWithContentsOfFile instead of imageNamed when loading xibs.

A third option might be to leave the UIImageViews in the xib 'blank' as a placeholder, and then programmatically loading UIImages into them later (for example in the FileOwner's init code), perhaps with the help of the 'tag' property + a dictionary to determine the right image, if it's convenient. Or you could even create both the images and their views dynamically instead of keeping them in the xib file. Obviously there are lots of tradeoffs here.

Merk
  • 1,441
  • 1
  • 15
  • 28
  • yes,I had the same thought as yours before,but when layout my view,it's not quite clear which control or imageView I'm layouting, of course I can clear the image property after layout,hmm,some solutions better?tks:) – user1610768 Sep 27 '12 at 08:09
  • Make an NSDictionary* dict that maps between integers (keys) and image names (values). In your xib, change the 'tag' property of each UIImageView to the integer corresponding to the image that's supposed to fill it. In your init method of your subclass, iterate through your UIImageViews like this: for (NSNumber* n in [dict allKeys]) { UIImageView* iview = [mySuperView viewWithTag:[n intValue]]; NSString* imagename = [dict valueForKey:n]; [iview initWithImage:...etc....] } Then you can load your UIImages however you like. – Merk Sep 27 '12 at 08:23
0
  1. Your solution isn't good :) Caching is used for good reasons.
  2. Just don't use [UIImage imageNamed:] In YOUR code, and all will be fine
  3. If you Actually need not to cache your images, you should use method #2.

NOTE: Images(actually, all objects) from XIBS are being loaded with -[object initWithCoder:], and not with [UIImage imageNamed:] or [UIImage imageWithContentsOfFile:]

tt.Kilew
  • 5,954
  • 2
  • 33
  • 51