【问题标题】:In initWithCoder: what are the keys in the NSCoder (a UINibDecoder)? (for UIImageView)在 initWithCoder 中:NSCoder(UINibDecoder)中的键是什么? (对于 UIImageView)
【发布时间】:2012-04-04 17:58:31
【问题描述】:

具体来说,我正在尝试查找图像路径。这将是一件非常有用的事情,据我所知,没有人知道如何获得。我查看了生成的 nib 文件中的密钥,并且可以在其中看到图像 URL (test.jpg),但找不到获取它的密钥。键“UIImage”返回实际已经构建的图像(通过initWithCGImageStored:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation 和一个神秘的UIKit 函数调用GetImageAtPath 调用上述init 调用),所以这没有帮助。

我还尝试使用 NSKeyedArchiver 将 UIImageView 写入磁盘,但这些值似乎都不正确,那里也不存在 test.jpg 值。

如果没有人能弄清楚 - 任何人都知道如何将二进制文件作为文本读取?我可以通读 nib 并解析出 URL,这总比没有好,但是无论我尝试什么格式,NSString 的构造函数都会失败。

【问题讨论】:

    标签: ios uikit interface-builder nscoder black-box


    【解决方案1】:

    您是否尝试过在 NSKeyedUnarchiver 上实现一个类别并以这种方式捕获键?快速实施和调查,您会发现关键是“UIResourceName”。但是请记住,keyed unarchiver 仅返回当前解码范围内的 key 对象。这意味着您无法从根中查询此键,您必须深入挖掘对象层次结构。

    下面是记录任何链接资源的代码。如何使用它取决于您。它还会记录 UIImage 何时被解码。如果你愿意,你可以在这里返回你自己的课程。

    @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
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多