【问题标题】:iOS - Archiving a UILabel with image as backgroundiOS - 以图像为背景归档 UILabel
【发布时间】:2011-12-22 20:16:10
【问题描述】:

我正在使用 colorWithPatternImage 设置 UILabel 的背景图像,但是当我将其归档时,我收到以下错误:

NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.'

是这样的,我想这是一个黑客。我的问题是:是否可以将图像存档为标签背景的一部分? 我出于不同的原因将 UILabel 子类化了,我可以添加什么来将图像设置为现有子类的背景吗?

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

NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:label];

其中 label 是具有背景图像集的 UILabel,使用 colorWithPatternImage。

干杯!

【问题讨论】:

  • 请显示一些会出现此错误的代码
  • 简单代码如:NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:label];如果 UILabel 的背景已使用前面提到的 colorWithPatternImage 设置。
  • 您能否更新原始问题并将代码与代码块一起添加到整个代码中?

标签: ios ipad core-data serialization uilabel


【解决方案1】:

【讨论】:

  • 我看了看,似乎是一个过度的解决方案。想知道子类化是否有更好的方法? IE。嵌入图像视图?
  • 我找不到更好的方法。也许您可以使用 Apple 的 DTS 票证来找到更简单、更优雅的解决方案。
【解决方案2】:

另一个选项是在您的 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

【讨论】:

  • 我已经考虑过这样做,因为这似乎是最好的,但是在尝试对图像进行编码时我得到了 BAD_ACCESS。我认为它不符合 NSCoding?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多