Core Image and memory leak, swift 3.0

1.3k views Asked by At

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

There are 1 answers

5
matt On BEST ANSWER

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.