使用子视图取消存档UIImageView [英] Unarchiving UIImageView with subviews

查看:83
本文介绍了使用子视图取消存档UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将UIImageView存档和取消存档会有些疯狂,该UIImageView包含许多子视图,这些子视图是从UIImageView派生的自定义视图.这是我所做的:

I'm going a bit crazy trying to archive and unarchive a UIImageView which has number of subviews which are custom views derived from UIImageView. Here's what I've done:

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

Add a category to the project to allow for the fact that UIImage does not conform to 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是主视图控制器的一个属性,称为背景".我这样保存它:

The UIImageView I'm archiving is a property of my main view controller called "background". I save it like this:

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

然后稍后我将其取消归档:

Then later I unarchive it like this:

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

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

Now I want to get my subviews (or one of them at least!) to display along with the unarchived root object:

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子类中的内容:

This is what I've added to my ORUImageView subclass:

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

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

I've tried this with and without the encoding for the image property.

我怀疑问题与该属性有关,因为我很明显将子视图添加到了未存档视图中-只是不可见!我觉得没有设置image属性,但是我找不到自己设置的方法,也找不到任何能告诉我正确方法的方法.

I'm suspect the problem is related to this property as I'm clearly getting the subview added to the unarchived view - it's just not visible! I feel the image property is not getting set, but I can't find a way to set it myself and I can't find anything that tells me the correct way to do this.

推荐答案

[先前的答案已删除.]

[Previous answer deleted.]

嗯.我刚刚尝试过:

- (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 已随其图像一起存档.因此,您必须尝试解决其他一些问题.但是我不明白问题出在哪里.

It worked - with an ordinary UIImageView. So now I guess I don't understand what you're getting at; it appears that a UIImageView is already archived with its image. So you must be trying to solve some other problem. But I'm not grasping what the problem is.

这篇关于使用子视图取消存档UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆