UIImageView에서 파생 된 사용자 정의보기 인 하위 뷰 수가있는 UIImageView를 보관하고 보관 취소하는 데 약간 미쳐 가고 있습니다. 여기에 내가 무슨 짓을했는지의 :하위 뷰를 사용하여 UIImageView를 보관 취소
이#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에 내 기본보기의 한 속성입니다 :
가있는 UIImage가 NSCoding을 준수하지 않는다는 사실 수 있도록 프로젝트에 카테고리 추가 컨트롤러는 "배경"이라고 불렀습니다. 나는이 같은 저장 :
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
지금 나는 (! 또는 적어도 그들 중 하나) 아카이브되지 함께 표시 내 파단 싶어 루트 개체는 :
- (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;
}
:
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 서브 클래스에 추가 한 것입니다
이미지 속성 인코딩을 사용하거나 사용하지 않고이 방법을 시도했습니다.
내가보기에 하위보기가 아카이브되지 않은보기에 추가되고있어 문제가이 속성과 관련되어있는 것으로 의심됩니다. 이미지 속성이 설정되지 않는다고 느낍니다. 그러나 직접 설정할 수있는 방법을 찾을 수 없으며이를 올바르게 수행 할 수있는 방법을 찾지 못합니다.
루트 객체를 다시 가져 와서 이미지를 표시하도록 관리하고 있습니다. 하지만 하위 뷰를 표시 할 수 없습니다. 보관 중이며 보관되지는 않지만 어디에 있습니까? 보관되지 않은 루트보기의 하위보기로 표시 할 수 없습니다. – beev
어쩌면 UIImage로하고있는 불필요한 것들 때문에. 위의 코드에서'iv'가 서브 뷰를 가지고 있다면 잘 동작합니다. – matt
그래, 고마워. self.view의 하위보기로 restoredImageView를 추가하면 작동합니다. 하지만 {self.background = restoredImageView}를 사용하면 아무 일도 발생하지 않습니다. (self.background는 IBOutlet UIImageView입니다.) 왜 내가 복원 된 ImageView를 self.background로 바꿀 수 없습니까? – beev