3

I have an app that consist of collectionview with 30 cell. Its cell consist of image and label. My problem is, when I load that image my memory use going 87 Mb. But if I didn't set that image, my memory use is only 30 Mb.

I am using Moa to load image asynchronously. And here is my cell init:

import UIKit
import moa

class ProductListItemCell: UICollectionViewCell {

    @IBOutlet var imgProductItem: UIImageView!
    @IBOutlet var titlleProductList: UILabel!
    @IBOutlet var priceProductList: UILabel!
    @IBOutlet var boldPriceProductList: UILabel!

    func updateContent(product: Item) {
//        if let imgUrl = product.picUrl {
//             self.imgProductItem.moa.url = "http:\(imgUrl)"
//        }

        if let title = product.title {
            self.titlleProductList.text = title
        }

        if let price = product.priceTag {
            self.priceProductList.setStrikethrough(price)
        }

        if let boldPrice = product.price {
            self.boldPriceProductList.text = boldPrice
        }
    }

}

And here is my cellForItemAtIndexPath

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if collectionView == myItemsCollectionView {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("firstCell", forIndexPath: indexPath) as! myCollectionViewCell

        cell.updateContent(self.myItems.items[indexPath.item])

        return cell
    }
    else {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("othersCell", forIndexPath: indexPath) as! myCollectionViewCell
        cell.updateContent(others[indexPath.section].items[indexPath.item])
        return cell
    }

}

Is there any way to download image and "compress" it? Or Am I doing it wrong? Pls let me know. Been stuck at this point for a weeks.

Note: How many memory use standard for some apps with collectionview like my apps? Does 30Mb is belongs to "high"? I am newbie in memory management.

Thank you so much.

Sonic Master
  • 1,238
  • 2
  • 22
  • 36
  • It depends, I don't know if this library **Moa** you mention keep a memory cache by default and for how much. You should learn first about this in its project page. Nevertheless I don't know why you use a library for download an image, and if you do try to use one that handles the reusability of the cells for you, like Kingfisher for example. – Victor Sigler Aug 24 '16 at 01:38
  • What are the dimensions of the images and what are the dimensions of the image view in the cell? The images should be no greater than image view dimensions (times, of course, the `scale` of the device). If they're bigger than that, you probably want to resize the images to match. – Rob Aug 24 '16 at 01:42
  • FYI, the runtime memory required for your images is generally 4 x `width` x `height` (those are the dimensions of the image, regardless of the size of the image view). – Rob Aug 24 '16 at 01:44
  • I can make a sure that those problem is caused by image. I already using alamofireImage and it still the same. – Sonic Master Aug 24 '16 at 03:39
  • Can I "resize" its image before place it into my UIImageView? My UIImageview is 136x136 size. – Sonic Master Aug 24 '16 at 03:40
  • Yep, you can use [this](http://stackoverflow.com/a/28513086/1271826) to resize the image. Or, if you're using Alamofire, you can use [AlamofireImage](https://github.com/Alamofire/AlamofireImage) which has nice `UIImageView` extension for async image retrieval, too). There are tons of image libraries that include image resizing. – Rob Aug 24 '16 at 03:55
  • Already using Alamofire and fetch image using `af_setImageWithURL`, but it still make a huge difference from 26Mb to 87Mb. Do you think I did something wrong? – Sonic Master Aug 24 '16 at 04:10
  • Are you using AlamofireImage's image resizing routines? – Rob Aug 24 '16 at 16:58
  • How to do that, rob? – Sonic Master Aug 24 '16 at 22:20

1 Answers1

2

Yes, you could use instrument to check memory leak and your memory increase caused by image load is most probably correct. I had this problem before. So what i did just use image caching library such as SDWebImage and it solve my problem as it's only require data for the first time fetch

xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • I already use AlamofireImage and set the image with `af_setImageWithURL` and it still the same. Gonna try your suggest with SDWebImage. – Sonic Master Aug 24 '16 at 03:43
  • `AlamofireImage` should work too as long you enable the image caching tihing – xmhafiz Aug 24 '16 at 03:47