3

I have two UIViewController in which the photos are displayed, the fact is that when I switch between them, then I add 50-70mb of RAM and so on ad infinitum.

image 1

image 2

'

var tags: [TagForRecipe] = [] // CoreData entity

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tagCell", for: indexPath) as! TagsTableViewCell

    cell.tags.text = tags[indexPath.row].nameTag
    cell.images.image = UIImage(contentsOfFile: (tags[indexPath.row].image as! URL).path)

    return cell
}



import UIKit

class TagsTableViewCell: UITableViewCell {


    @IBOutlet weak var images: UIImageView!

    @IBOutlet weak var tags: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

'

2 Answers2

1

I don't see any memory leak in your graph. It just shows the amount of the memory used so far. Memory leak is the second line, and if there is one it's marked with red colour. For more information check this link

If your images are too large, then try to resize them. This will improve the performance and your tableview will be smooth while scrolling.

aytek
  • 1,842
  • 24
  • 32
  • But the memory is not cleaned, but hangs forever – Alexander Govorukhin Feb 28 '18 at 18:37
  • @AlexanderGovorukhin I don't think you have a memory problem, If you had you would receive a memory warning and your app would be closed. Please follow my advice and try to reduce the size of the images. [This](https://stackoverflow.com/a/28566113/1259775) might also help – aytek Mar 01 '18 at 15:03
  • @AlexanderGovorukhin You can use [this](https://stackoverflow.com/questions/31314412/how-to-resize-image-in-swift) for resizing images – aytek Mar 01 '18 at 22:42
1

You should set your image using contentOfFiles not from storyboard. Delete your image outlet and the image in storyboard and then use this code in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    if let splashPath = Bundle.main.path(forResource: "bg", ofType: "png") {
        backgroundImgView.image = UIImage.init(contentsOfFile: splashPath)
    }
}
Kawe
  • 659
  • 8
  • 18