- [UIImage的resizableImageWithCapInsets:]奇怪的行为

- [UIImage的resizableImageWithCapInsets:]奇怪的行为

问题描述:

在我的应用我做背景用简单的纯彩色标签编程,如:- [UIImage的resizableImageWithCapInsets:]奇怪的行为

- (UIImage*)badgeImage 
{ 
static UIImage *image = nil; 
static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(30, 20), NO, 0.0f); 
    [RGB(231, 112, 63) setFill]; 
    [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 30, 20) cornerRadius:10] fill]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
}); 
return image; 

}

然后我调整此图片所需的尺寸与-resizableImageWithCapInsets:和申请结果作为标签背景:

CGSize someSize = MY_SIZE; 
UIImage *resizableImage = [badgeImage resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f)]; 
UIGraphicsBeginImageContextWithOptions(someSize, NO, 0); 
[resizableImage drawInRect:CGRectMake(0, 0, someSize.width, someSize.height)]; 
UIImage *backImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
[label setFrameSize:someSize]; 
[label setBackgroundColor:[UIColor colorWithPatternImage:backImage]]; 

但是,这给了我奇怪的结果:enter image description here我不知道在哪里从这个垂直线和如何避免它。

+0

嗯怪异......我只是复制了你的代码,对我来说它的工作原理没有垂直线是可见的。你的'someSize'和'size'值是什么?你如何初始化UILabel? –

+0

@TamaraBernad'size' =='someSize',这是我的错字。当然,UILabel是从xib(storyboard)初始化的,它只是隐藏在屏幕截图上。 – Akki

+0

我明白UILabel已经初始化。我的意思是说如何初始化看起来像'someSize'的值是什么,看看我能否重现你的输出。 –

通过将resizableImage置入UIImageView解决问题,该框架与label具有相同的框架。

在我的情况下,下面帮(已被UIGraphicsBeginImageContextWithOption后调用)

CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), NO);