iOS - 归档以图像为背景的 UILabel

发布于 2024-12-22 13:36:39 字数 505 浏览 1 评论 0原文

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

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

是的,我猜这是一个 hack。我的问题是:是否可以将图像存档作为标签背景的一部分? 我出于不同的原因对 UILabel 进行了子类化,是否可以添加任何内容以便将图像设置为现有子类的背景?

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

NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:label];

其中 label 是使用 colorWithPatternImage 设置背景图像的 UILabel。

干杯!

I am using colorWithPatternImage to set the background image of a UILabel but, when I come to archive it, I get the following error:

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

Which is the case, I guess it is a hack. My question is: is it possible to archive an image as part of a labels background?
I have subclassed UILabel for a different reason, is there anything I can add in order to set an image as a background with the existing subclass?

For clarity, this is the code that causes the problem:

NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:label];

Where label is a UILabel that has a background image set, using colorWithPatternImage.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-12-29 13:36:39

另一种选择是在 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

Another option is in your UILabel subclass, you create an ivar to store the pattern image and you archived the ivar. When you unarchive your UILabel subclass, you recreate the pattern image with the image ivar.

Sample code for the label.

@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

Sampel View Controller

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文