【发布时间】:2021-09-22 06:02:19
【问题描述】:
iOS 14.5,Swift 版本 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)
一直想弄清楚为什么这段代码不起作用?这个 iOS/Swift 版本的 Xcode 模拟器上的过滤器是否简单损坏
private func generateMono(from image: UIImage) ->
UIImage {
let mono = CIFilter(name: "CIColorMonochrome")
mono!.setDefaults()
mono!.setValue(image.cgImage, forKey: "inputImage")
if let image = mono!.outputImage {
if let image = context.createCGImage(image, from: image.extent) {
return UIImage(cgImage: image)
}
}
return UIImage(systemName: "circle") ?? UIImage()
}
我运行此程序并收到此错误消息以及系统转储。
2021-07-13 09:17:45.910734+0200 GameV[32820:1828401] The filter 'CIPortraitEffectSpillCorrection' is not implemented in the bundle at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/CoreImage/PortraitFilters.cifilter.
2021-07-13 09:17:45.911776+0200 GameV[32820:1828401] Metal GPU Frame Capture Enabled
2021-07-13 09:17:45.912087+0200 GameV[32820:1828401] Metal API Validation Enabled
2021-07-13 09:17:46.363938+0200 GameV[32820:1828401] -[__NSCFType imageByUnpremultiplyingAlpha]: unrecognized selector sent to instance 0x7f941bd15700
2021-07-13 09:17:46.377114+0200 GameV[32820:1828401] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType imageByUnpremultiplyingAlpha]: unrecognized selector sent to instance 0x7f941bd15700'
我在这里错过了阅读/做一些明显错误的事情吗?用这段代码调用它?
import Foundation
import SpriteKit
import CoreImage.CIFilterBuiltins
class GameScene: SKScene {
var img:UIImage!
let context = CIContext(options: nil)
static var shared = GameScene()
override func didMove(to view: SKView) {
backgroundColor = .white
let mono = generateMono(from: uiImage!) as? UIImage
let tex = SKTexture(image: mono!)
let box = SKSpriteNode(texture: tex, size: CGSize(width: 64, height: 64))
box.position = CGPoint(x: 128, y: 128)
addChild(box)
}
}
谢谢
【问题讨论】:
-
您的源图像是“图像”。并且您将输出设置为“图像”?为什么要强制展开单声道?
-
我向你保证 CIFilter 不会突然坏掉。
-
应该如下所示。 func generateMono(来自图像:UIImage)-> UIImage? { if let filter = CIFilter(name: "CIColorMonochrome") { if let ciImage = CIImage(image: image) { filter.setValue(ciImage, forKey: "inputImage") filter.setValue(1.00, forKey: "inputIntensity") if让 output = filter.outputImage { return UIImage(ciImage: output) } } } return nil }