问题描述
我只需要裁剪 UIImage。我有 CVPixelBuffer 图像。
我是按照这个方法解决的。
将 CVPixelBuffer 转换为 UIImage 并裁剪 UIImage
为此使用了两种方法。
1)
CV -> CI -> UI -> CG -> UI(裁剪)。
但是在这里我得到了错误,因为 ui 不能直接转换为 CGImage。为此,我使用了 createCGImage 但此方法存在内存泄漏。
2)
CV -> CI -> UI -> UI(裁剪)。
但是这个方法有内存泄漏。
3)
CV -> CV(cropped) -> CI -> UI.
结果是空白屏幕图像。
对于方法 1,我使用了这些代码。
func convertCVtoUI(image: CVPixelBuffer) -> UIImage {
let image2 = (CIImage(cvPixelBuffer: image))
let image3 = UIImage(ciImage: image2)
return image3
}
这个用于转换
var ciImage = CIImage(image: image)
if (ciImage == nil){
ciImage = image.ciImage
}
let context = CIContext(options: nil)
let cgImage = context.createCGImage(ciImage!,from: ciImage!.extent)
let cropedImage = cgImage!.cropping(to: cropRect)
return UIImage(cgImage: cropedImage!)
这个用于裁剪裁剪的作品,但有内存泄漏
对于方法 2,我使用了这些代码。
转换为 UI 代码是一样的。
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
let drawRect = CGRect(x: -rect.origin.x,y: -rect.origin.y,width: image.size.width,height: image.size.height)
context?.clip(to: CGRect(x: 0,y: 0,width: rect.size.width,height: rect.size.height))
image.draw(in: drawRect)
let subImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return subImage!
裁剪有效,但有内存泄漏。
对于方法 3
crop function
并在顶部转换为具有相同功能的UI
如何做到这一点而不会出现内存泄漏?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)