gpt4 book ai didi

ios - 如何对 Playground CIFilter 错误消息进行故障排除

转载 作者:行者123 更新时间:2023-11-28 06:08:59 26 4
gpt4 key购买 nike

下面的代码应该可以正常运行:不是我写的! :-)

我尝试运行 Simon J. Gladman (https://github.com/FlexMonkey) 编写的一些 Swift Playground 代码,这些代码对应于他的书 Core Image for Swift 中的第 8 章“Warp Kernels”。当他在 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 代表“信号中止”之外,我不知道它是什么意思,更重要的是,我不知道如何排除错误。 (我还在学习 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

最佳答案

Playground 不喜欢 inputImage 未声明为 @objc。如果你改变这个:

class CRTWarpFilter: CIFilter
{
var inputImage: CIImage?

为此:

class CRTWarpFilter: CIFilter
{
@objc var inputImage: CIImage?

它会正常工作。

这是更新到 Xcode 11Swift 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

关于ios - 如何对 Playground CIFilter 错误消息进行故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47266302/

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