我遇到了一个奇怪的问题,即在从照片库中裁剪照片后,应用程序无法显示它。运行以下代码后,它会给出以下错误:
以下是我用来裁剪和显示图片的代码。(inputImage 是 CIImage 类型)
self.correctedImageView.image = UIImage(ciImage: correctedImage)
[api] -[CIContext(CIRenderDestination) _startTaskToRender:toDestination:forPrepareRender:error:] The image extent and destination extent do not intersect.
let imageSize = inputImage.extent.size
let correctedImage = inputImage
.cropped(to: textObvBox.boundingBox.scaled(to: imageSize) )
DispatchQueue.main.async {
self.correctedImageView.image = UIImage(ciImage: correctedImage)
}
更多信息:调试打印输入图像和更正后的图像的范围。以下是self.inputImage的描述:
CIImage: 0x1c42047a0 的范围为 [0 0 3024 4032]。
剪裁 [430 3955 31 32] 的范围为 [430 3955 31 32],仿射变换 [0 -1 1 0 0 4032] 的范围为 [0 0 3024 4032],不透明。仿射变换 [1 0 0 -1 0 3024] 的范围为 [0 0 4032 3024],不透明。颜色匹配“sRGB IEC61966-2.1”_to_workingspace 范围为 [0 0 4032 3024],不透明。IOSurface 0x1c4204790(501) seed:1 YCC420f 601 alpha_one 的范围为 [0 0 4032 3024],不透明。
有趣的是,当我设置断点时,使用Xcode,我能够正确地预览裁剪后的图像。我不确定CIImage中的这个范围是什么意思,但当我将裁剪后的图像分配给UIImageView时,它似乎不太喜欢它。您知道这个范围是做什么的吗?
.cropped(to:)
是在设置范围。您将其设置为textObvBox.boundingBox.scaled(to: imageSize)
。 - Lily BallardCIContext
将CIImage
渲染为位图。 - Lily BallardCIContext
- 注意,这些很昂贵(即:创建一个需要处理时间),尝试只创建一次并将其用于所有操作 - 然后我使用CIContext.createCGImage(CIImage, from: CIExtent)
,最后使用UIImage(cgImage:)
。我选择这种方法是因为我使用GLKView
(使用GPU)而不是UIImageView
(使用CPU)将东西渲染到屏幕上。如果编码正确,单个CIContext
就足够了,不需要UIImage
。 - user7014451