Swift playground - 无法将过滤后的UIImage转换为CGImage

时间:2016-05-02 01:03:39

标签: swift uiimage cgimage ciimage

在Swift游乐场中,我正在加载JPEG,将其转换为UIImage并将其过滤为单色。 然后我将生成的过滤图像转换为UIImage。

输入和过滤后的图像显示正确。

然后我将两个图像转换为CGImage类型。 这适用于输入图像,但过滤后的图像从转换中返回nil:

// Get an input image
let imageFilename = "yosemite.jpg"
let inputImage = UIImage(named: imageFilename )
let inputCIImage = CIImage(image:inputImage!)

// Filter the input image - make it monochrome
let filter = CIFilter(name: "CIPhotoEffectMono")
filter!.setDefaults()
filter!.setValue(inputCIImage, forKey: kCIInputImageKey)
let CIout = filter!.outputImage
let filteredImage = UIImage(CIImage: CIout!)        

// Convert the input image to a CGImage
let inputCGImageRef = inputImage!.CGImage           // Result: <CGImage 0x7fdd095023d0>
// THE LINE ABOVE WORKS

// Try to convert the filtered image to a CGImage
let filteredCGImageRef = filteredImage.CGImage      // Result: nil
// THE LINE ABOVE DOES NOT WORK
// Note that the compiler objects to 'filteredImage!.CGImage'

出了什么问题?

1 个答案:

答案 0 :(得分:5)

UIImageCIImage创建的CGImage不是由let context = CIContext() let filteredCGImageRef = context.createCGImage( CIout!, fromRect: CIout!.extent) 支持的。您需要明确创建一个:

UIImage

如果您需要CGImage,请从CIContext呈现的UIImage(CGImage: filteredCGImageRef) 创建<:<}

Wzorce = Wzorce + UserForm1.Controls(CStr(j)).Caption

干杯,

西蒙