In initWithCoder: what are the keys in NSCoder (UINibDecoder)? (for UIImageView)

In particular, I am trying to find the path to the image. That would be a very useful thing to get, and as far as I know, no one knows how to do this. I looked at the generated nib file for keys, and I can see the URL of the image there (test.jpg), but I can not find the key to receive it. The "UIImage" key returns the actual image already built (constructed via initWithCGImageStored:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation and the cryptic call to the UIKit GetImageAtPath function, which calls the above init call), so this is not useful.

I also tried writing UIImageView to disk using NSKeyedArchiver, and none of these values ​​seem correct, and test.jpg does not exist.

If no one can understand this - does anyone know how to read in binary as text? I could just read the nib and parse the urls, which is better than nothing, but the NSString constructors do not work no matter what format I'm trying to execute.

+7
source share
1 answer

Have you tried to inject a category into NSKeyedUnarchiver and catch the keys this way? Quick implementation and research, and you will find that the key is "UIResourceName". However, keep in mind that keyarch unarchiver returns only objects for the key in the current decoding area. This means that you cannot request this key from the root, and you need to delve deeply into the hierarchy of objects.

Below is the code that registers any related resources. It is up to you how you use it. It is also registered when the UIImage is decoded. You can return your own class if you want.

 @interface NSKeyedUnarchiver (MyKeyedUnarchiver) @end @implementation NSKeyedUnarchiver (MyKeyedUnarchiver) - (id) mydecodeObjectForKey:(NSString *)key { id obj = [self mydecodeObjectForKey:key]; if ( [key isEqualToString:@"UIResourceName"] ) NSLog(@"The linked resource name is: %@", obj); return obj; } - (Class) myclassForClassName:(NSString *)codedName { if ( [codedName isEqualToString:@"UIImageNibPlaceholder"] ) NSLog(@"Decoding a UIImage"); return [self myclassForClassName:codedName]; } + (void) load { SEL origM = @selector( decodeObjectForKey: ); SEL newM = @selector( mydecodeObjectForKey: ); method_exchangeImplementations( class_getInstanceMethod( self, origM ), class_getInstanceMethod( self, newM ) ); origM = @selector( classForClassName: ); newM = @selector( myclassForClassName: ); method_exchangeImplementations( class_getInstanceMethod( self, origM ), class_getInstanceMethod( self, newM ) ); } @end 

Hope this helps.

+5
source

All Articles