gpt4 book ai didi

ios - swift 中的 Core Image TileEffect 过滤器返回 nil 图像

转载 作者:行者123 更新时间:2023-11-29 00:17:18 27 4
gpt4 key购买 nike

我正在为 CI 过滤器创建演示应用程序,并且几乎应用了所有类别的所有过滤器。但是当我尝试在 CICategoryTileEffect 中添加过滤器时,除了“CIKaleidoscope”之外,所有过滤器都返回 nil 图像。所有其他过滤器都崩溃了。这是显示代码的类的一部分。

enum EN_TileEffect :String
{
case affineClamp = "AffineClamp"
case affineTile = "AffineTile"
case eightfoldReflectedTile = "EightfoldReflectedTile"
case fourfoldReflectedTile = "FourfoldReflectedTile"
case fourfoldRotatedTile = "FourfoldRotatedTile"
case fourfoldTranslatedTile = "FourfoldTranslatedTile"
case kaleidoscope = "Kaleidoscope"
case opTile = "OpTile"
case parallelogramTile = "ParallelogramTile"
case perspectiveTile = "PerspectiveTile"
case sixfoldReflectedTile = "SixfoldReflectedTile"
case triangleKaleidoscope = "TriangleKaleidoscope"
case triangleTile = "TriangleTile"
case twelvefoldReflectedTile = "TwelvefoldReflectedTile"
}

class TileEffect {

fileprivate static var filter : CIFilter!
fileprivate static var context : CIContext = CIContext(options:nil)

static func applyFilter(filter: EN_TileEffect , image: UIImage) -> UIImage {
switch filter {
case .affineClamp: return affineClamp(image: image)
case .kaleidoscope: return kaleidoscope(image: image)
default : return image
}
}

//Just working fine and as expected
fileprivate static func kaleidoscope(image:UIImage) -> UIImage {

let originalImage = CIImage(cgImage: (image.cgImage)!)
let orientation = (image.imageOrientation)

filter = CIFilter(name: "CIKaleidoscope")
filter?.setValue(originalImage, forKey: kCIInputImageKey)
filter?.setValue(CIVector(x:120,y:120), forKey: kCIInputCenterKey)
filter?.setValue(0, forKey: kCIInputAngleKey)
filter?.setValue(2, forKey: "inputCount")

let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)
return UIImage(cgImage: cgimg!, scale: 1.0, orientation: orientation)
}
// This method could not generate a new image and crashing every time.
fileprivate static func affineClamp(image:UIImage) -> UIImage {

let originalImage = CIImage(cgImage: (image.cgImage)!)
let orientation = (image.imageOrientation)

filter = CIFilter(name: "CIAffineClamp")
filter?.setValue(originalImage, forKey: kCIInputImageKey)

let transform = CGAffineTransform.identity
let val = NSValue(cgAffineTransform: transform)
filter.setValue(val, forKey: "inputTransform")

**//Always returning nil image here**
let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)

return UIImage(cgImage: cgimg!, scale: 1.0, orientation: orientation)
}
}

我搜索了很多但没有找到任何解决方案或工作示例。

最佳答案

谢谢大家。替换一行代码解决了我的问题来自

let cgimg = context.createCGImage(filter!.outputImage!, from: filter!.outputImage!.extent)

 let cgimg = context.createCGImage(filter!.outputImage!, from: originalImage.extent)

关于ios - swift 中的 Core Image TileEffect 过滤器返回 nil 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012806/

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