gpt4 book ai didi

ios - 在 Swift 中使用 CIColorMatrix 滤镜

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:10 24 4
gpt4 key购买 nike

以下 Swift 函数应该使用指定的“tintColor”对灰度图像“greyImage”进行着色:

import UIKit

func colorizeImage(greyImage : UIImage, tintColor : UIColor) -> UIImage? {

let colorMatrixFilter = CIFilter(name: "CIColorMatrix")

var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
tintColor.getRed(&r, green:&g, blue:&b, alpha:&a)

colorMatrixFilter.setDefaults()
colorMatrixFilter.setValue(greyImage, forKey:"inputImage") //kCIInputImageKey)
colorMatrixFilter.setValue(CIVector(x:r, y:0, z:0, w:0), forKey:"inputRVector")
colorMatrixFilter.setValue(CIVector(x:0, y:g, z:0, w:0), forKey:"inputGVector")
colorMatrixFilter.setValue(CIVector(x:0, y:0, z:b, w:0), forKey:"inputBVector")
colorMatrixFilter.setValue(CIVector(x:0, y:0, z:0, w:a), forKey:"inputAVector")

if let ciImage = colorMatrixFilter.outputImage {
return UIImage(CIImage: ciImage);
}

return nil;
}

颜色是 UIColor.orangeColor() (r=1,g=0.5,b=0,a=1),灰度图像是可以的,因为它在输入到 ImageView 时显示正常。

看起来所有必要的 key 都已提供并且 key 分配顺利进行(顺便说一句,过滤器是检查 key 的有效性,还是接受所有内容?),但是读取“outputImage”属性会产生 SIGABRT 和以下控制台消息:

2015-05-02 13:04:07.319 MyApp[436:8241] -[UIImage _internalRepresentation]: unrecognized selector sent to instance 0x7fd5b3ca82b0
2015-05-02 13:04:07.629 MyApp[436:8241] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage _internalRepresentation]: unrecognized selector sent to instance 0x7fd5b3ca82b0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001087abf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a56cbb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001087b304d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010870b27c ___forwarding___ + 988
4 CoreFoundation 0x000000010870ae18 _CF_forwarding_prep_0 + 120
5 CoreImage 0x0000000108bd30fe -[CIColorMatrix outputImage] + 885
6 MyApp 0x00000001085c182d _TF8MyApp13colorizeImageFTCSo7UIImageCSo7UIColor_GSqS0__ + 4733
7 MyApp 0x00000001085c2b59

最佳答案

问题是 CIColorMatrix期望参数 inputImage 应该是 CIImage 对象而不是 UIImage (greyImage)。

更新:Swift 3 或更高版本

extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
var (r,g,b,a): (CGFloat,CGFloat,CGFloat,CGFloat) = (0,0,0,0)
return getRed(&r, green: &g, blue: &b, alpha: &a) ? (r,g,b,a) : nil
}
}

extension CIImage {
var image: UIImage { .init(ciImage: self) }
func colorized(with color: UIColor) -> CIImage? {
guard
let (r,g,b,a) = color.rgba,
let colorMatrix = CIFilter(name: "CIColorMatrix",
parameters: ["inputImage": self,
"inputRVector": CIVector(x: r, y: 0, z: 0, w: 0),
"inputGVector": CIVector(x: 0, y: g, z: 0, w: 0),
"inputBVector": CIVector(x: 0, y: 0, z: b, w: 0),
"inputAVector": CIVector(x: 0, y: 0, z: 0, w: a)])
else { return nil }
return colorMatrix.outputImage
}
}

extension UIImage {
var coreImage: CIImage? { CIImage(image: self) }
func colorized(with color: UIColor) -> UIImage? {
coreImage?.colorized(with: color)?.image
}
}

Playground 测试:

let profilePicture = UIImage(data: try! Data(contentsOf: URL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!))!
profilePicture.colorized(with: .orange)

enter image description here

关于ios - 在 Swift 中使用 CIColorMatrix 滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997985/

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