gpt4 book ai didi

ios - 从图像文件创建 CIImage 模式

转载 作者:可可西里 更新时间:2023-11-01 05:42:53 27 4
gpt4 key购买 nike

我试图了解什么是最有效的方法来创建一个基于图像文件的带有模式的CIImage

let patternImage = UIImage(named: "pattern.png")

我采用的第一种方法:

// First create UIColor pattern
let uiColorPattern = UIColor(patternImage: patternImage!)

// Than init CIColor using the UIColor pattern object
let ciColorPattern = CIColor(color: uiColorPattern)

// Finally, create CIImage using the CIColor initialiser
let ciImagePattern = CIImage(color: ciColorPattern) //

遗憾的是,由于未知原因,CIImage 只是空白。我还尝试应用 clampedToExtent() 并对其进行裁剪,但它仍然是空白的。

我采用的第二种方法(有效但速度太慢):

UIGraphicsBeginImageContext(size) // create context

patternImage?.drawAsPattern(in: rect) // draw pattern as 'UIImage'

let patternUIImage = UIGraphicsGetImageFromCurrentImageContext() // fetch 'UIImage'

UIGraphicsEndImageContext() // end context

if let patternUIImage = patternUIImage {
let ciImagePattern = CIImage(image: patternUIImage) // Create 'CIImage' using 'UIImage'
}

此方法有效,但正如我所说,太慢了。


总的来说,我的直觉是,如果我能让第一种方法奏效,它将比第二种方法更有效。

任何其他方法/建议将不胜感激!

最佳答案

您采用的第一种方法行不通来自 WWDC 2013:

So, because, you know, and CIColor doesn't handle color spaces, it won't do CMYK color, it won't do patterns.

So, these are all things that you're going to lose.

If you ever think that you're going to need that, then probably not the right strategy.

所以第二种方法是我们可以优化的。我有一个返回图案图像的片段,您可以试一试。

        let myImage = UIImage.init(named: "www")
let imageSize = myImage?.size
let imageWidth = imageSize?.width
let imageHeight = imageSize?.height
let widthIterations = self.view.bounds.size.width / imageWidth!
let heightIterations = self.view.bounds.size.height / imageHeight!

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()


for i in 0 ... Int(heightIterations) {
let yPoint = CGFloat(i) * imageHeight!
for j in 0 ... Int(widthIterations) {
let xPoint = CGFloat(j) * imageWidth!
myImage?.draw(at: CGPoint.init(x: xPoint, y: yPoint))
}
}
let tempImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

enter image description here

Further reading

关于ios - 从图像文件创建 CIImage 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47837925/

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