ios - 取消归档带有 subview 的 UIImageView

标签 ios uiimageview nsdata nskeyedarchiver nscoder

我试图归档和取消归档一个 UIImageView ,它有很多 subview ,这些 subview 是从 UIImageView 派生的自定义 View ,我有点疯狂。这是我所做的:

向项目添加一个类别,以允许 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 是我的主视图 Controller 的一个属性,称为“背景”。我这样保存:

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

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

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;
}

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

我怀疑问题与此属性有关,因为我清楚地将 subview 添加到未归档 View 中 - 它只是不可见!我觉得图像属性没有设置,但我找不到自己设置它的方法,也找不到任何告诉我执行此操作的正确方法的内容。

最佳答案

嗯。我刚刚尝试过这个:

- (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 已与其图像一起存档。所以你一定是在尝试解决其他问题。但我不明白问题出在哪里。

关于ios - 取消归档带有 subview 的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334453/

相关文章:

html - 从 UIImage 数据创建 URL

ios - 无法在 XCode 7 beta 2 中调试

ios - 在应用程序中使用自定义框架的settings.bundle

ios - 为什么我的类明明有 NSManagedObject 属性却出现错误?

ios - 如何获得屏幕上 UIview 的百分比

ios - 防止垂直 UIStackView 拉伸(stretch) subview ?

objective-c - 一段时间后 NSData 变为 null

ios - 获取从 iPhone 获取的 Gif 图像的 NSData 时无法获取 Gif 图像帧数

iphone - NSJSONSerialization 不创建键值对

objective-c - 获取从目录加载的 NSString 中的文件名