通过UIImage
从CIImgae
获取UIImage(ciImage:)
似乎是一个非常老的问题,但这仍然是一个问题。这是stackoverflow上的一个旧线程,它提出了相同的问题:old thread
我曾经猜想这个问题应该可以修复iOS的6个主要版本(我当前运行的是iOS 12.0的代码),但事实并非如此。这是一个示例游乐场代码,供您尝试并重现该问题:
import Foundation
import UIKit
import PlaygroundSupport
// extend UIImage to support cunstruction of solid color images
extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1), contentScaleFactor: CGFloat = UIScreen.main.scale) {
let rect = CGRect(origin: CGPoint.zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, contentScaleFactor)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage, scale: contentScaleFactor, orientation: .up)
}
}
let uIImage = UIImage(color: UIColor.red, size: CGSize(width: 100.0, height: 100.0), contentScaleFactor: 1.0)!
let cIImage = CIImage(image: uIImage)!
let finalUIImage = UIImage(ciImage: cIImage)
let imageView = UIImageView(image: finalUIImage)
PlaygroundPage.current.liveView = imageView
在let finalUIImage = UIImage(ciImage: cIImage)
行出现错误的地方
无法将UIImage转换为png
如先前提到的关于stackoverflow的旧帖子answer所述,用{p1
let finalUIImage = UIImage(ciImage: cIImage)
有效。
我要发布此问题是因为我想知道
(在XCode 10.1 beta 3和XCode 10.0上进行了测试)