추가 속성 및 메서드를 포함하려는 UIImage (UIImageExtra) 하위 클래스를 만들었습니다.사용자 지정 개체의 NSArray 저장
이 사용자 지정 클래스의 인스턴스를 포함하는 배열이 있습니다. 그러나 배열을 저장할 때 UIImageExtra 클래스의 추가 데이터가 저장되지 않는 것으로 보입니다.
UIImageExtra는 NSCoding을 준수하지만 추가 한 NSLog 문은 인쇄되지 않으므로 initWithCoder 또는 encodeWithCoder도 호출되지 않습니다.
배열을 저장하는 내 방법은 다음과 같습니다
- (void)saveIllustrations {
if (_illustrations == nil) {
NSLog(@"Nil array");
return;
}
[self createDataPath];
//Serialize the data and write to disk
NSString *illustrationsArrayPath = [_docPath stringByAppendingPathComponent:kIllustrationsFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_illustrations forKey:kIllustrationDataKey];
[archiver finishEncoding];
[data writeToFile:illustrationsArrayPath atomically: YES];
}
을 그리고 UIImageExtra 절약에 대해 다음과 위임 방법이 있습니다 : 첫 번째 장소 외모의 배열을 만들 수
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
NSLog(@"Encoding origin data!");
[super encodeWithCoder:aCoder];
[aCoder encodeObject:originData forKey:kOriginData];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:(NSCoder *) aDecoder]) {
NSLog(@"Decoding origin data");
self.originData = [aDecoder decodeObjectForKey:kOriginData];
}
return self;
}
내 코드를 이런 경우 (단서를 제공하는 경우)
for (NSDictionary *illustrationDict in illustrationDicts) {
NSString *illustrationString = [illustrationDict objectForKey:@"Filename"];
NSNumber *xCoord = [illustrationDict objectForKey:@"xCoord"];
NSNumber *yCoord = [illustrationDict objectForKey:@"yCoord"];
UIImageExtra *illustration = (UIImageExtra *)[UIImage imageNamed:illustrationString];
//Scale the illustration to size it for different devices
UIImageExtra *scaledIllustration = [illustration adjustForResolution];
NSValue *originData = [NSValue valueWithCGPoint:CGPointMake([xCoord intValue], [yCoord intValue])];
[scaledIllustration setOriginData:originData];
[self.illustrations addObject:scaledIllustration];
}
아니면 그냥 이 데이터를 잘못된 방식으로 보내고 있습니까? 많은 감사합니다.
나는 + (UIImageExtra *) imageExtraWithUIImage : (UIImage *) 이미지에 어떤 것이 들어가는 지 잘 모르겠다. UIImage 인수로 무엇을합니까? alloc 및 init 새 UIImageExtra 개체를 만들고 그것을 반환하는 가정 해요하지만 UIImage 해당 새로 할당 된 UIImageExtra 속성으로 설정해야합니까? 어떤 경우 하위 클래스에서 UIImage의 인스턴스를 정의해야합니까? 부모 클래스의 인스턴스를 하위 클래스의 속성으로 가지는 것이 이상한가요? 또는 어떻게 든 부모 클래스를 UIImage의 인스턴스로 설정할 수 있습니까? – Smikey