如何对 Playground CIFilter 错误消息进行故障排除
How to Troubleshoot Playground CIFilter Error Message
下面的代码应该运行没问题:不是我写的! :-)
我尝试了 运行宁一些 Swift Simon J. Gladman (https://github.com/FlexMonkey) 的游乐场代码,对应于他的书 Core Image 中的第 8 章,"Warp Kernels,"对于 Swift。当他在 Xcode 7.2 中编写此代码而我在 Xcode 9.1 中将其 运行 时,我不得不在几个地方更新它,在大多数情况下这相当于仅仅接受错误处理程序(或任何所谓的)的建议。这样做之后,出现了以下听起来更严重的消息:
error: Execution was interrupted, reason: signal SIGABRT.
The process has been left at the point where it was interrupted, use "thread >return -x" to return to the state before expression evaluation.
除了知道 SIGABRT 代表 "signal abort," 之外,我不知道它是什么意思,更重要的是,我不知道如何排除错误。 (我还在学习 Swift 和 Cocoa/Cocoa Touch。。。。)建议将不胜感激。
//: ## Barrel Distortion Warp Filter
import UIKit
import CoreImage
//: ### Warp Kernel
class CRTWarpFilter: CIFilter
{
var inputImage: CIImage?
var bend: CGFloat = 3.2
let crtWarpKernel = CIWarpKernel(source:
"kernel vec2 crtWarp(vec2 extent, float bend)" +
"{" +
" vec2 coord = ((destCoord() / extent) - 0.5) * 2.0;" +
" coord.x *= 1.0 + pow((abs(coord.y) / bend), 2.0);" +
" coord.y *= 1.0 + pow((abs(coord.x) / bend), 2.0);" +
" coord = ((coord / 2.0) + 0.5) * extent;" +
" return coord;" +
"}"
)
override var outputImage: CIImage!
{
if let inputImage = inputImage, let crtWarpKernel = crtWarpKernel
{
let arguments = [CIVector(x: inputImage.extent.size.width,
y: inputImage.extent.size.height),
bend] as [Any]
let extent = inputImage.extent
return crtWarpKernel.apply(extent: extent,
roiCallback:
{(index, rect) in return rect},
image: inputImage,
arguments: arguments)
}
return nil
}
}
let ciContext = CIContext()
func imageFromCIImage(source: CIImage) -> UIImage
{
let cgImage = ciContext.createCGImage(source, from: source.extent)
return UIImage(cgImage: cgImage!)
}
//: ### Swift Implementation of barrel warp kernel
//: `x` and `y` are pixel coordinates
let x = 65.0
let y = 55.0
//: `width` and `height` are extent
let width = 900.0
let height = 300.0
//: `crtWarpKernel` mechanics in Swift
var coordX = ((x / width) - 0.5) * 2.0
var coordY = ((y / height) - 0.5) * 2.0
coordX *= 1 + pow((abs(coordY) / 3.2), 2.0)
coordY *= 1 + pow((abs(coordX) / 3.2), 2.0)
coordX = ((coordX / 2.0) + 0.5) * width
coordY = ((coordY / 2.0) + 0.5) * height
// ----
let backgroundImage = CIFilter(name: "CICheckerboardGenerator",
withInputParameters: [
"inputColor0":
CIColor(red: 0.1, green: 0.1, blue: 0.1),
"inputColor1":
CIColor(red: 0.15, green: 0.15, blue: 0.15),
"inputCenter": CIVector(x: 0, y: 0),
"inputWidth": 50])!
.outputImage!.cropped(to: CGRect(x: 1, y: 1,
width: width - 2,
height: height - 2))
.composited(over: CIImage(color: CIColor(red: 0, green: 0, blue: 0)))
.cropped(to: CGRect(origin: CGPoint.zero,
size: CGSize(width: width, height: height)))
let blueBox = CIImage(color: CIColor(red: 0.5, green: 0.5, blue: 1, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: coordX - 5, y: coordY - 5),
size: CGSize(width: 10, height: 10)))
let redBox = CIImage(color: CIColor(red: 1, green: 0, blue: 0, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: x - 5, y: y - 5),
size: CGSize(width: 10, height: 10)))
let warpFilter = CRTWarpFilter()
warpFilter.inputImage = backgroundImage
let composite = CIFilter(name: "CIAdditionCompositing",
withInputParameters: [
kCIInputBackgroundImageKey: warpFilter.outputImage,
kCIInputImageKey: blueBox])!
.outputImage!
.applyingFilter("CIAdditionCompositing",
parameters: [kCIInputBackgroundImageKey: redBox])
let result = composite
操场不喜欢 inputImage
未声明为 @objc。如果你改变这个:
class CRTWarpFilter: CIFilter
{
var inputImage: CIImage?
为此:
class CRTWarpFilter: CIFilter
{
@objc var inputImage: CIImage?
它会很好地工作。
您的代码已更新为 Xcode 11
和 Swift 5
//: ## Barrel Distortion Warp Filter
import UIKit
import CoreImage
//: ### Warp Kernel
class CRTWarpFilter: CIFilter
{
@objc var inputImage: CIImage?
var bend: CGFloat = 3.2
let crtWarpKernel = CIWarpKernel(source: """
kernel vec2 crtWarp(vec2 extent, float bend)
{
vec2 coord = ((destCoord() / extent) - 0.5) * 2.0;
coord.x *= 1.0 + pow((abs(coord.y) / bend), 2.0);
coord.y *= 1.0 + pow((abs(coord.x) / bend), 2.0);
coord = ((coord / 2.0) + 0.5) * extent;
return coord;
}
""")
override var outputImage: CIImage?
{
if let inputImage = inputImage, let crtWarpKernel = crtWarpKernel
{
let arguments = [CIVector(x: inputImage.extent.size.width,
y: inputImage.extent.size.height),
bend] as [Any]
let extent = inputImage.extent
return crtWarpKernel.apply(extent: extent,
roiCallback:
{(index, rect) in return rect},
image: inputImage,
arguments: arguments)
}
return nil
}
}
let ciContext = CIContext()
func imageFromCIImage(source: CIImage) -> UIImage
{
let cgImage = ciContext.createCGImage(source, from: source.extent)
return UIImage(cgImage: cgImage!)
}
//: ### Swift Implementation of barrel warp kernel
//: `x` and `y` are pixel coordinates
let x = 65.0
let y = 55.0
//: `width` and `height` are extent
let width = 900.0
let height = 300.0
//: `crtWarpKernel` mechanics in Swift
var coordX = ((x / width) - 0.5) * 2.0
var coordY = ((y / height) - 0.5) * 2.0
coordX *= 1 + pow((abs(coordY) / 3.2), 2.0)
coordY *= 1 + pow((abs(coordX) / 3.2), 2.0)
coordX = ((coordX / 2.0) + 0.5) * width
coordY = ((coordY / 2.0) + 0.5) * height
// ----
let backgroundImage = CIFilter(name: "CICheckerboardGenerator",
parameters: [
"inputColor0":
CIColor(red: 0.1, green: 0.1, blue: 0.1),
"inputColor1":
CIColor(red: 0.15, green: 0.15, blue: 0.15),
"inputCenter": CIVector(x: 0, y: 0),
"inputWidth": 50])!
.outputImage!.cropped(to: CGRect(x: 1, y: 1,
width: width - 2,
height: height - 2))
.composited(over: CIImage(color: CIColor(red: 0, green: 0, blue: 0)))
.cropped(to: CGRect(origin: CGPoint.zero,
size: CGSize(width: width, height: height)))
let blueBox = CIImage(color: CIColor(red: 0.5, green: 0.5, blue: 1, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: coordX - 5, y: coordY - 5),
size: CGSize(width: 10, height: 10)))
let redBox = CIImage(color: CIColor(red: 1, green: 0, blue: 0, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: x - 5, y: y - 5),
size: CGSize(width: 10, height: 10)))
let warpFilter = CRTWarpFilter()
warpFilter.inputImage = backgroundImage
let composite = CIFilter(name: "CIAdditionCompositing",
parameters: [
kCIInputBackgroundImageKey: warpFilter.outputImage,
kCIInputImageKey: blueBox])!
.outputImage!
.applyingFilter("CIAdditionCompositing",
parameters: [kCIInputBackgroundImageKey: redBox])
let result = composite
下面的代码应该运行没问题:不是我写的! :-)
我尝试了 运行宁一些 Swift Simon J. Gladman (https://github.com/FlexMonkey) 的游乐场代码,对应于他的书 Core Image 中的第 8 章,"Warp Kernels,"对于 Swift。当他在 Xcode 7.2 中编写此代码而我在 Xcode 9.1 中将其 运行 时,我不得不在几个地方更新它,在大多数情况下这相当于仅仅接受错误处理程序(或任何所谓的)的建议。这样做之后,出现了以下听起来更严重的消息:
error: Execution was interrupted, reason: signal SIGABRT. The process has been left at the point where it was interrupted, use "thread >return -x" to return to the state before expression evaluation.
除了知道 SIGABRT 代表 "signal abort," 之外,我不知道它是什么意思,更重要的是,我不知道如何排除错误。 (我还在学习 Swift 和 Cocoa/Cocoa Touch。。。。)建议将不胜感激。
//: ## Barrel Distortion Warp Filter
import UIKit
import CoreImage
//: ### Warp Kernel
class CRTWarpFilter: CIFilter
{
var inputImage: CIImage?
var bend: CGFloat = 3.2
let crtWarpKernel = CIWarpKernel(source:
"kernel vec2 crtWarp(vec2 extent, float bend)" +
"{" +
" vec2 coord = ((destCoord() / extent) - 0.5) * 2.0;" +
" coord.x *= 1.0 + pow((abs(coord.y) / bend), 2.0);" +
" coord.y *= 1.0 + pow((abs(coord.x) / bend), 2.0);" +
" coord = ((coord / 2.0) + 0.5) * extent;" +
" return coord;" +
"}"
)
override var outputImage: CIImage!
{
if let inputImage = inputImage, let crtWarpKernel = crtWarpKernel
{
let arguments = [CIVector(x: inputImage.extent.size.width,
y: inputImage.extent.size.height),
bend] as [Any]
let extent = inputImage.extent
return crtWarpKernel.apply(extent: extent,
roiCallback:
{(index, rect) in return rect},
image: inputImage,
arguments: arguments)
}
return nil
}
}
let ciContext = CIContext()
func imageFromCIImage(source: CIImage) -> UIImage
{
let cgImage = ciContext.createCGImage(source, from: source.extent)
return UIImage(cgImage: cgImage!)
}
//: ### Swift Implementation of barrel warp kernel
//: `x` and `y` are pixel coordinates
let x = 65.0
let y = 55.0
//: `width` and `height` are extent
let width = 900.0
let height = 300.0
//: `crtWarpKernel` mechanics in Swift
var coordX = ((x / width) - 0.5) * 2.0
var coordY = ((y / height) - 0.5) * 2.0
coordX *= 1 + pow((abs(coordY) / 3.2), 2.0)
coordY *= 1 + pow((abs(coordX) / 3.2), 2.0)
coordX = ((coordX / 2.0) + 0.5) * width
coordY = ((coordY / 2.0) + 0.5) * height
// ----
let backgroundImage = CIFilter(name: "CICheckerboardGenerator",
withInputParameters: [
"inputColor0":
CIColor(red: 0.1, green: 0.1, blue: 0.1),
"inputColor1":
CIColor(red: 0.15, green: 0.15, blue: 0.15),
"inputCenter": CIVector(x: 0, y: 0),
"inputWidth": 50])!
.outputImage!.cropped(to: CGRect(x: 1, y: 1,
width: width - 2,
height: height - 2))
.composited(over: CIImage(color: CIColor(red: 0, green: 0, blue: 0)))
.cropped(to: CGRect(origin: CGPoint.zero,
size: CGSize(width: width, height: height)))
let blueBox = CIImage(color: CIColor(red: 0.5, green: 0.5, blue: 1, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: coordX - 5, y: coordY - 5),
size: CGSize(width: 10, height: 10)))
let redBox = CIImage(color: CIColor(red: 1, green: 0, blue: 0, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: x - 5, y: y - 5),
size: CGSize(width: 10, height: 10)))
let warpFilter = CRTWarpFilter()
warpFilter.inputImage = backgroundImage
let composite = CIFilter(name: "CIAdditionCompositing",
withInputParameters: [
kCIInputBackgroundImageKey: warpFilter.outputImage,
kCIInputImageKey: blueBox])!
.outputImage!
.applyingFilter("CIAdditionCompositing",
parameters: [kCIInputBackgroundImageKey: redBox])
let result = composite
操场不喜欢 inputImage
未声明为 @objc。如果你改变这个:
class CRTWarpFilter: CIFilter
{
var inputImage: CIImage?
为此:
class CRTWarpFilter: CIFilter
{
@objc var inputImage: CIImage?
它会很好地工作。
您的代码已更新为 Xcode 11
和 Swift 5
//: ## Barrel Distortion Warp Filter
import UIKit
import CoreImage
//: ### Warp Kernel
class CRTWarpFilter: CIFilter
{
@objc var inputImage: CIImage?
var bend: CGFloat = 3.2
let crtWarpKernel = CIWarpKernel(source: """
kernel vec2 crtWarp(vec2 extent, float bend)
{
vec2 coord = ((destCoord() / extent) - 0.5) * 2.0;
coord.x *= 1.0 + pow((abs(coord.y) / bend), 2.0);
coord.y *= 1.0 + pow((abs(coord.x) / bend), 2.0);
coord = ((coord / 2.0) + 0.5) * extent;
return coord;
}
""")
override var outputImage: CIImage?
{
if let inputImage = inputImage, let crtWarpKernel = crtWarpKernel
{
let arguments = [CIVector(x: inputImage.extent.size.width,
y: inputImage.extent.size.height),
bend] as [Any]
let extent = inputImage.extent
return crtWarpKernel.apply(extent: extent,
roiCallback:
{(index, rect) in return rect},
image: inputImage,
arguments: arguments)
}
return nil
}
}
let ciContext = CIContext()
func imageFromCIImage(source: CIImage) -> UIImage
{
let cgImage = ciContext.createCGImage(source, from: source.extent)
return UIImage(cgImage: cgImage!)
}
//: ### Swift Implementation of barrel warp kernel
//: `x` and `y` are pixel coordinates
let x = 65.0
let y = 55.0
//: `width` and `height` are extent
let width = 900.0
let height = 300.0
//: `crtWarpKernel` mechanics in Swift
var coordX = ((x / width) - 0.5) * 2.0
var coordY = ((y / height) - 0.5) * 2.0
coordX *= 1 + pow((abs(coordY) / 3.2), 2.0)
coordY *= 1 + pow((abs(coordX) / 3.2), 2.0)
coordX = ((coordX / 2.0) + 0.5) * width
coordY = ((coordY / 2.0) + 0.5) * height
// ----
let backgroundImage = CIFilter(name: "CICheckerboardGenerator",
parameters: [
"inputColor0":
CIColor(red: 0.1, green: 0.1, blue: 0.1),
"inputColor1":
CIColor(red: 0.15, green: 0.15, blue: 0.15),
"inputCenter": CIVector(x: 0, y: 0),
"inputWidth": 50])!
.outputImage!.cropped(to: CGRect(x: 1, y: 1,
width: width - 2,
height: height - 2))
.composited(over: CIImage(color: CIColor(red: 0, green: 0, blue: 0)))
.cropped(to: CGRect(origin: CGPoint.zero,
size: CGSize(width: width, height: height)))
let blueBox = CIImage(color: CIColor(red: 0.5, green: 0.5, blue: 1, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: coordX - 5, y: coordY - 5),
size: CGSize(width: 10, height: 10)))
let redBox = CIImage(color: CIColor(red: 1, green: 0, blue: 0, alpha: 0.7))
.cropped(to: CGRect(origin: CGPoint(x: x - 5, y: y - 5),
size: CGSize(width: 10, height: 10)))
let warpFilter = CRTWarpFilter()
warpFilter.inputImage = backgroundImage
let composite = CIFilter(name: "CIAdditionCompositing",
parameters: [
kCIInputBackgroundImageKey: warpFilter.outputImage,
kCIInputImageKey: blueBox])!
.outputImage!
.applyingFilter("CIAdditionCompositing",
parameters: [kCIInputBackgroundImageKey: redBox])
let result = composite