I'm developing a Comic Reader App, and by now, I'm close to having a MVP, but I'm facing an issue which I've been struggling for weeks and I can't solve.
I've tried a lot of different things, but none of them seem to resolve the issue.
I'll put you in context:
When the user taps on a comic, the App pushes a ViewController that has a UICollectionView that displays each page of the comic, and the user can swipe between pages.
If the user taps on this UICollectionView, two toolbars appear:
Top Toolbar: A toolbar displaying the name of the comic, and a button to exit the ViewController
Bottom Toolbar: A toolbar displaying a UICollectionView that displays all of the pages that there are on the comic:
Well, this particular UICollectionView, is consuming a LOT of RAM Memory and I don't know why.
I'm using KingFisher and the code to render every UIImage inside the UICollectionViewCells is as follows:
I create the cell like this in the UICollectionView:
func cellForPage(at indexPath: IndexPath) -> UICollectionViewCell { if let cell = mainCV.dequeueReusableCell(withReuseIdentifier: kSmallPageCVC, for: indexPath) as? SmallPageCVC { let page = self.pages[indexPath.row] cell.configure(page: page) return cell } else { return UICollectionViewCell() } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { return cellForPage(at: indexPath) }
Then, in the "cell.configure" method, I set the image using the URL I passed by parameter:
func configure(page: URL) { self.page.kf.setImage(with: page, placeholder: nil, options: nil, progressBlock: nil) { [weak self] (result: Result<RetrieveImageResult, KingfisherError>) in switch result { case .failure(let error): print(error) case .success(let imageResult): self?.page.image = imageResult.image } } }
Well, this takes a LOT of RAM memory, up to 1GB of RAM (if the comic size is big), to the point that the App crashes. Why is it taking so much RAM memory, even if I use the "prepareForReuse" method, setting the image to nil?
Is there any way to optimize the memory allocation to be able to render the images without taking so much RAM memory?
I find it very ridiculous for it to use so much memory.
All of the Apps like Whatsapp, Twitter, Instagram, Facebook, Telegram, even other Comic Reader Apps, show several images with full resolution without crashing the App.
Could someone give me a hint here please?
I heard about the autoreleasepool method, but I don't think it is appropiate to use it, and I think it is a far more optimum way to display those images without allocating so much memory. So please, avoid suggesting that on the comments. Or, if you suggest it, explain to me why are you suggesting this, and why is the best option in order for me to use it.
I used native Swift too in order to set the images, using UIImage(contentsOf: myUrl), but the results are the same, plus, the UICollectionView freezes when I scroll. So please, avoid suggesting that too on the comments.
Best regards,
Thanks.