gpt4 book ai didi

iOS UIImage 蒙版边框

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:11 25 4
gpt4 key购买 nike

在 iOS 中有没有办法为不是简单矩形的图像添加边框?

我已经使用以下代码成功地为图像着色:

- (UIImage *)imageWithTintColor:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);

CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);

[tintColor setFill];
CGContextFillRect(context, rect);

UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return tintedImage;
}

举个例子,我想给这张图片添加一个蓝色边框(注意:这不是“A”NSString,而是一个 UIImage 对象示例)

enter image description here

当我将上面的代码更改为 [color setStroke]CGContextStrokeRect(context, rect) 时,图像就消失了。

我已经从 SO 了解到,使用 CoreImage + EdgeDetection 是可能的,但不是有一个“简单”的 CoreGraphics - 类似于给图像着色的方式吗?

谢谢!

-- 编辑 --

请注意,我想为图像本身添加边框。我不想通过 UIImageView 创建边框效果!

在应用边框之前,边框应该与图像的形状相匹配。在这种情况下:'A' 外部 + 内部的蓝色轮廓。

最佳答案

我认为这不是一个非常令人满意的方法,但在某种程度上是有效的。

您可以使用向图层添加阴影。为此,您需要去除图像中的白色部分,让字符被 alpha 包围。

我使用了下面的代码。

UIImage *image = [UIImage imageNamed:@"image_name_here.png"];

CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, 200.0f, 200.0f);
imageLayer.contents = (id)image.CGImage;
imageLayer.position = self.view.layer.position;

imageLayer.shadowColor = [UIColor whiteColor].CGColor;
imageLayer.shadowOffset = CGSizeMake(0.0f, 0.0f);
imageLayer.shadowOpacity = 1.0f;
imageLayer.shadowRadius = 4.0f;

[self.view.layer addSublayer:imageLayer];

结果会是这样的。

And the result would be

关于iOS UIImage 蒙版边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com