Core Image and memory leak, swift 3.0

拜拜、爱过 提交于 2019-12-08 12:23:03

问题


i have problem i try use filter at some images which have extension 3000x2000 , when i do it RAM upper and app have fatal error the "didReceiveMemoryWarning".

 func setFilters(images: [UIImage]) -> [UIImage] {
    let filter = CIFilter(name: "CIColorControls")!
    filter.setValue(2.0, forKey: kCIInputContrastKey)

    let context = CIContext(options: nil)

    var result = [UIImage]()

    for img in images {
        let newImage = autoreleasepool(invoking: { () -> UIImage in
            filter.setValue(CIImage(image: img)!, forKey: kCIInputImageKey)

            let ciImage = filter.outputImage!
            let cgImage = context.createCGImage(ciImage, from: ciImage.extent)


            return UIImage(cgImage: cgImage!, scale: img.scale, orientation: img.imageOrientation)
        })

        result.append(newImage)
    }

    return result
}

回答1:


It's not a memory leak; it's that you are in fact using too much memory. And it's not the use of CIFilter that's causing the problem; it's the fact that you are trying to keep all of these huge UIImage objects in memory in a single array:

var result = [UIImage]()
// ...
result.append(newImage)

Don't do that.



来源:https://stackoverflow.com/questions/41333823/core-image-and-memory-leak-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!