私は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];
:私はこのようにそれを保存する(!または少なくともそれらの1つ)
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;
}
イメージプロパティのエンコードの有無にかかわらず試しました。
私は明らかにサブビューがアーカイブされていないビューに追加されているので、このプロパティに関連していると思われます。私はイメージのプロパティが設定されていないと感じるが、私はそれを自分で設定する方法を見つけることができませんし、私はこれを行う正しい方法を教えて何かを見つけることができません。
私はルートオブジェクトを取得して、そのイメージを表示するように管理しています。しかし私はそのサブビューを表示することができません。彼らはアーカイブされており、アーカイブされていませんが、どこにあるのですか?私はそれらをアーカイブされていないルートビューのサブビューとして表示することはできません。 – beev
あなたがUIImageでやっている不必要なもののためかもしれません。上記のコードで 'iv'がサブビューを持っていれば、うまく動作します。 – matt
ありがとう、それは理にかなっています。あなたが持っているように、self.viewのサブビューとしてrestoredImageViewを追加すると動作します。しかし、{self.background = restoredImageView}を使用しても何も起こりません。 (self.backgroundはIBOutlet UIImageViewです)。何故私は復元されたImageViewをself.backgroundに入れ替えることができないのでしょうか? – beev