【问题标题】:CIFilters seems to be broken on on iOS 14, Swift 5.xCIFilters 似乎在 iOS 14、Swift 5.x 上被破坏了
【发布时间】: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 }

标签: swift swiftui cifilter


【解决方案1】:

我在 SwiftUI 项目中使用了这个 UIImage 扩展,它工作正常:

import CoreImage
import CoreImage.CIFilterBuiltins

extension UIImage {
    
    func applyFilter() -> UIImage {
        let beginImage = CIImage(image: self )
        let context = CIContext()
        
        let customFilter = CIFilter.colorMonochrome()
        customFilter.inputImage = beginImage
        customFilter.intensity = 1
        
        guard  let outputImage = customFilter.outputImage else { return UIImage() }
        
        // attempt to get a CGImage from our CIImage
        if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
            let filteredImage = UIImage(cgImage: cgimg)
            return filteredImage
        }
        return UIImage()
    }
}

用法:

struct ContentView: View {
    var body: some View {
        Image(uiImage: (UIImage(named: "img3")?.applyFilter())!)
    }
}

【讨论】:

  • 一个叫 'CIFilter.colorMonochrome()' 的家伙是从哪里来的?
  • @ElTomato 它在 CoreImage 框架中。只需跳转到定义并查看。 ('colorMonochrome()' 仅适用于 iOS 13.0 或更高版本)
【解决方案2】:

它在 cmets 中,但这里是关于如何设置对 CIColorMonochrome 的呼叫的完整(和更好的格式)答案:

let filter = CIFilter(name: "CIColorMonochrome")
let context = CIContext()
var extent: CGRect!
var scaleFactor: CGFloat!

@IBOutlet weak var img: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    let ciImage = CIImage(image: img.image!)

    // Note: you may use kCIInputImageKey for inputImage
    filter?.setValue(ciImage, forKey: "inputImage")
    filter?.setValue(Float(1), forKey: "inputAngle")
    let result = filter?.outputImage

    var image = UIImage(cgImage: context.createCGImage(result!, from: result!.extent)!)
    img.image = image

}

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2016-02-01
    • 2021-01-09
    相关资源
    最近更新 更多