iOS-以图像为背景归档UILabel

iOS-以图像为背景归档UILabel,ios,ipad,core-data,serialization,uilabel,Ios,Ipad,Core Data,Serialization,Uilabel,我正在使用colorWithPatternImage设置UILabel的背景图像,但当我归档它时,出现以下错误: NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.' 是这样的,我想这是一个黑客。我的问题是:是否可以将图像归档为标签背景的一部分? 我之所以将UILabel子类化是因为另一个原因,我是否可以添加任何东西,以便将图

我正在使用colorWithPatternImage设置UILabel的背景图像,但当我归档它时,出现以下错误:

NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.'
是这样的,我想这是一个黑客。我的问题是:是否可以将图像归档为标签背景的一部分? 我之所以将UILabel子类化是因为另一个原因,我是否可以添加任何东西,以便将图像设置为现有子类的背景

为清楚起见,这是导致问题的代码:

NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:label];
其中label是使用colorWithPatternImage设置背景图像的UILabel


干杯

听起来你和我有同样的问题


另一个选项是在UILabel子类中,创建一个ivar来存储模式图像,然后归档ivar。当您取消UILabel子类的归档时,您将使用图像ivar重新创建模式图像

标签的示例代码

@implementation ESKLabelArchive

@synthesize backgroundImage=_backgroundImage;

#pragma mark - NSCoding Protocols
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        self.backgroundImage = (UIImage *)[aDecoder decodeObjectForKey:@"backgroundImage"];
        if (self.backgroundImage != nil)
            self.backgroundColor = [UIColor colorWithPatternImage:self.backgroundImage];

    }
    return self;
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = [backgroundImage copy];
    self.backgroundColor = [UIColor colorWithPatternImage:_backgroundImage];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    if (self.backgroundImage != nil)
    {
        self.backgroundColor = [UIColor clearColor];
        [aCoder encodeObject:self.backgroundImage forKey:@"backgroundImage"];
    }
    [super encodeWithCoder:aCoder];
    if (self.backgroundImage != nil)
    {
        self.backgroundColor = [UIColor colorWithPatternImage:self.backgroundImage];
    }
}



@end
采样视图控制器

@implementation ESKViewController

@synthesize label;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (IBAction)archivedTapped:(id)sender
{
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:self.label forKey:@"label"];
    [archiver finishEncoding];

    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    ESKLabelArchive *label2 = [unarchiver decodeObjectForKey:@"label"];
    [unarchiver finishDecoding];

    label2.text = @"unarchived";
    label2.frame = CGRectMake(20, 150, 200, 100);

    [self.view addSubview:label2];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.label.backgroundImage = [UIImage imageNamed:@"ricepaper.png"];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.label = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

请显示一些给出此错误的代码简单代码,如:NSData*viewData=[NSKeyedArchiver archivedDataWithRootObject:label];如果UILabel的背景已设置为前面提到的颜色WithPatternImage。能否请您更新原始问题并添加完整的代码块?我看了一下,这似乎是一个过度的解决方案。想知道子类化是否有更好的方法?例如,嵌入图像视图?我找不到更好的方法。也许你可以使用苹果的DTS标签来找到一个更简单、更优雅的解决方案。我已经考虑过这样做,因为这似乎是最好的方法,但我在尝试编码图像时遇到了一个糟糕的访问。我想它不符合NSCoding?