我正试着在PNG周围创建一个笔触边框,背景透明,为NSImage.

我最初试着复制图像,用更大的比例,然后用CIFilter把它变成白色--这对圆形和其他实心形状很管用.

但是,我将拥有类似下面的示例图像的形状:

Tree Image

对于这个图像,它根本不能很好地工作.

我在想,也许我可以用CIEdge和CIMorphologyMaximum做点什么,但我不确定我的 idea 是否正确--如果有人遇到类似的挑战,我会感激一些建议.

推荐答案

是的,CIMorphologyMaximum应该是个不错的 Select .试试这个:

import CoreImage.CIFilterBuiltins

let ciImage = ...

// Apply morphology maximum to "erode" image in all direction into transparent area.
let filter = CIFilter.morphologyMaximum()
filter.inputImage = ciImage
filter.radius = 5 // border radius
let eroded = filter.outputImage!

// Turn all pixels of eroded image into desired border color.
let colorized = CIBlendKernel.sourceAtop.apply(foreground: .white, background: eroded)!.cropped(to: eroded.extent)

// Blend original image over eroded, colorized image.
let imageWithBorder = ciImage.composited(over: colorized)

而在Objective-C年:

CIImage* ciImage = ...;

// Apply morphology maximum to "erode" image in all direction into transparent area.
CIFilter* erodeFilter = [CIFilter filterWithName:@"CIMorphologyMaximum"];
[erodeFilter setValue:ciImage forKey:kCIInputImageKey];
[erodeFilter setValue:@5 forKey:kCIInputRadiusKey];
CIImage* eroded = erodeFilter.outputImage;

// Turn all pixels of eroded image into desired border color.
CIImage* colorized = [[CIBlendKernel.sourceAtop applyWithForeground:[CIImage whiteImage] background:eroded] imageByCroppingToRect:eroded.extent];

// Blend original image over eroded, colorized image.
CIImage* imageWithBorder = [ciImage imageByCompositingOverImage:colorized];

Objective-c相关问答推荐

try 从 id 确定类的类型时,Objective-C消息传递不合格的 id

我应该为 memcpy 和 realloc 包含什么标题?

UIApplication.sharedApplication.delegate.window 和 UIApplication.sharedApplication.keyWindow 有什么区别?

在 Interface Builder 中设计 UITableView 的section header

NSLocalizedString 格式

在 NSData 和 base64 字符串之间转换

启动 Finder 窗口并 Select 特定文件

在基于视图的 NSTableView 上更改 Select colored颜色

Facebook 添加测试用户和管理员是:(待定),这是什么意思?

iPhone - 时区便利方法之间的差异

iPhone 键盘、完成按钮和 resignFirstResponder

如何在 Objective C 中将浮点数转换为 int?

判断 NSString 是否仅包含字母数字 + 下划线字符

强制iOS应用程序崩溃的最快方法是什么?

在 iOS UILabel 上设置 BOLD 字体

@dynamic 在 Objective-C 中做了什么?

UITableView ,重新加载时滚动到底部?

如何让 UITextView 检测网站、邮件和电话号码的链接

在 iOS 6.0 的 Xcode 4.5 中我没有要求的日志(log)消息

如何在 Objective-c 中将数组声明为常量?