0

I'm building a game that uses some images, and I load them in UIImageViews. By making some tests, I realized that the memory increases when I load the images, but do not decrease when I remove the UIImageView from superview.

Example:
- initial memory: 50mb
- then I load some images in many UIImageViews (that are inside a UIView)
- the memory goes to about 70mb
- remove all UIImageViews from superview, and then remove the UIView
- the memory keeps 70mb

It seems like the loaded PNG (in this case, I use assets to storage all of them) keeps in memory/cached, and only "decache" when the app closes.

How can I resolve this? When the app goes to about 70-80mb, the app starts to become slower, and it is very frustrating.

LeMag
  • 53
  • 1
  • 8
  • 1
    It would help if you post some code. Are you downsizing your PNG's to an appropriate size for the device? (i.e. no point putting a 4000x4000 image in the asset catalog, if you are only going to display it with 200x200 dimensions. You might also want to look at autoreleasepool. https://stackoverflow.com/questions/25860942/is-it-necessary-to-use-autoreleasepool-in-a-swift-program – rbaldwin May 06 '18 at 07:24
  • The code I use is basically load the UIImageView with UIImage(named: filename). I'm testing now contentsOfFile. The files have only the needed size for the device. – LeMag May 06 '18 at 07:45

1 Answers1

0

You can use this code it will not store the image in cache

extension UIImageView {
    func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleToFill) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {
                self.image = image
            }
            }.resume()
    }
    func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleToFill ) {
        guard let url = URL(string: link) else { return }
        downloadedFrom(url: url, contentMode: mode)
    }
    func downloadimageFrom(url: URL, contentMode mode: UIViewContentMode = .scaleToFill) {
        contentMode = mode
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard
                let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
                let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
                let data = data, error == nil,
                let image = UIImage(data: data)
                else { return }
            DispatchQueue.main.async() {

                self.image = image

            }
            }.resume()
    }
    func downloadimageFrom(link: String, contentMode mode: UIViewContentMode = .scaleToFill ) {
        guard let url = URL(string: link) else { return }
        downloadimageFrom(url: url, contentMode: mode)
    }


}
kunwar11
  • 123
  • 1
  • 1
  • 10