OGeek|极客世界-中国程序员成长平台

标题: ios - - [UIImage resizableImageWithCapInsets :] strange behavior [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:08
标题: ios - - [UIImage resizableImageWithCapInsets :] strange behavior

在我的应用程序中,我以编程方式使用简单的纯色为标签制作背景,例如:

- (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我不知道这条垂直线从哪里来以及如何避免它。



Best Answer-推荐答案


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

关于ios - - [UIImage resizableImageWithCapInsets :] strange behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21263177/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4