【问题标题】:Unarchiving UIImageView with subviews使用子视图取消归档 UIImageView
【发布时间】:2012-03-09 05:15:18
【问题描述】:

尝试归档和取消归档 UIImageView 有点疯狂,该 UIImageView 具有许多子视图,这些子视图是从 UIImageView 派生的自定义视图。这是我所做的:

为项目添加一个类别以允许 UIImage 不符合 NSCoding 的事实:

#import "UIImage-NSCoding.h"
#define kEncodingKey @"UIImage"

@implementation UIImage(NSCoding)

- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super init]))
    {
        NSData *data = [decoder decodeObjectForKey:kEncodingKey];
        self = [self initWithData:data];
    }        
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    NSData *data = UIImagePNGRepresentation(self);
    [encoder encodeObject:data forKey:kEncodingKey];
}

@end

我正在归档的 UIImageView 是我的主视图控制器的一个属性,称为“背景”。我这样保存:

NSData *dataToSave = [NSKeyedArchiver archivedDataWithRootObject:self.background];
[dataToSave dataWithContentsOfFile:imagePath options:NSDataWritingAtomic error:nil];

然后我像这样解压它:

NSData *savedData = [NSData dataWithContentsOfFile:imagePath];    
UIImageView *restoredImageView = [NSKeyedUnarchiver unarchiveObjectWithData:savedData];
self.background.image = restoredImageView.image; // this works - archived image is displayed

现在我想让我的子视图(或至少其中一个!)与未归档的根对象一起显示:

NSLog(@"Subviews: %d", [restoredImageView.subviews count]); // this tells me my subviews have been archived along with the root object
NSLog(@"Subview 0: %@", [restoredImageView.subviews objectAtIndex:0]); // and they exist as expected

NSLog(@"Subviews: %d", [self.background.subviews count]); // at this point no subviews are present
[self.background addSubview:[restoredImageView.subviews objectAtIndex:0]]; // subview is not visible on screen
NSLog(@"Subviews: %d", [self.background.subviews count]); // count is one, but where is it?

ORUImageView *oru = [[ORUImageView alloc] init]; // I try creating the subview myself
oru = [self.background.subviews objectAtIndex:0]; 
[self.background addSubview:oru]; // it still doesn't show on screen
[self.background bringSubviewToFront:oru]; // makes no difference

NSLog(@"String: %@", oru.testString); // this correctly logs the test string I added to my custom class

这是我添加到我的 ORUImageView 子类的内容:

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];    
    [aCoder encodeObject:self.testString forKey:@"theString"];
    [aCoder encodeObject:self.image forKey:@"theImage"]; // not sure if this is needed
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    [self setTestString:[aDecoder decodeObjectForKey:@"theString"]];
    [self setImage:[aDecoder decodeObjectForKey:@"theImage"]]; // not sure if this is needed
    return self;
}

我已经尝试过使用和不使用 image 属性的编码。

我怀疑问题与此属性有关,因为我清楚地将子视图添加到未归档视图中 - 它只是不可见!我觉得 image 属性没有被设置,但我找不到自己设置的方法,也找不到任何告诉我正确方法的东西。

【问题讨论】:

    标签: ios uiimageview nsdata nskeyedarchiver nscoder


    【解决方案1】:

    [之前的答案已删除。]

    编辑:嗯。我刚试过这个:

    - (IBAction)doButton1:(id)sender {
        theData = [NSKeyedArchiver archivedDataWithRootObject:iv];
        [iv removeFromSuperview];
        iv = nil;
    }
    
    - (IBAction)doButton2:(id)sender {
        iv = [NSKeyedUnarchiver unarchiveObjectWithData:theData];
        [self.view addSubview:iv];
    }
    

    它工作 - 与一个普通的 UIImageView。所以现在我想我不明白你在说什么;似乎 UIImageView is 已经与其图像一起存档。因此,您必须尝试解决其他问题。但我不明白问题出在哪里。

    【讨论】:

    • 我正在取回根对象并设法显示其图像。但我无法显示它的子视图。它们正在被归档,它们正在被取消归档,但它们在哪里?我无法让它们显示为未归档根视图的子视图。
    • 可能是因为您对 UIImage 进行了不必要的操作。如果上面代码中的iv 有子视图,它对我来说很好。
    • 好的,谢谢,这是有道理的。如果我像您一样将restoredImageView 添加为self.view 的子视图,它会起作用。但是如果我使用 { self.background = restoreImageView } 什么都不会发生。 (self.background 是一个 IBOutlet UIImageView)。知道为什么我不能将 restoreImageView 换成 self.background 吗?
    • 好的,谢谢。我将不得不对此做更多的实验......
    • 好吧,我向您展示的代码是一个完整的示例:它从界面中取出 UIImageView 并将其归档,然后将其解压缩并放入界面中。它与 UIImageView 的子视图及其图像一起作为归档/取消归档的一部分。所以我建议你从那个开始。
    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 2021-04-30
    • 1970-01-01
    • 2010-11-19
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多