swift - Memory issues with UIImage Animation Image Caching -
i've been banging head against days , can't bottom of it. have keyboard extension uses uicollectionview display animated uiimages. when it's running , switch app switch keyboard i'm getting spike around 10mb. crashes app after few swaps.
i've gone through instruments , looks i'm getting heaps of uiimages when keyboard reloads, 'eventually' cleaned takes while , accumulative effect max's out memory. think it's uiimage caching animation image start around 150 - 200 uiimages, correlates total animation frames have across cells. pretty doubles every reload.
i've thrown cell prepareforreuse:
override func prepareforreuse() { if imageview?.isanimating() == true { nslog("stop animating") imageview?.stopanimating() } imageview?.animationimages = nil contentview.subviews.map({$0.removefromsuperview()}) imagerect = cgrect(x: 0, y: 0, width: frame.size.width, height: frame.size.height) imageview = uiimageview(frame: imagerect!) type = nil super.prepareforreuse() imageview?.contentmode = uiviewcontentmode.scaleaspectfit contentview.addsubview(imageview!) }
but no avail. isn't getting called on reload. so, new cells being created, old ones aren't being cleared maybe?
i've tried running this:
func clearcellimages(){ cell in collectionview.visiblecells(){ cell.imageview!?.animationimages = nil cell.imageview!!.image = nil } }
on viewdiddisappear , deinit, no difference.
it took me soooooo long find issue, , have, i'm stumped. happy post code think help, happy pointed in right direction.
thanks.
so turned out 2 pronged solution. first made these little helper functions:
func clearcells(){ cell in collectionview.visiblecells(){ clearcell(cell as! mycellview) } } func clearcell(cell:mycellview){ if let iv = cell.imageview { iv.animationimages = nil iv.image = nil iv.removefromsuperview() } }
i called clearcells
on viewwilldisappear
, , saw drop in uiimages
, memory use, however, if had scrolled, more likely, images had been used in cells not visible not de-referenced, resulting in small, not insignificant memory creep. fixed calling clearcell
on didenddisplayingcell
delegate method:
func collectionview(collectionview: uicollectionview, didenddisplayingcell cell: uicollectionviewcell, foritematindexpath indexpath: nsindexpath) { clearcell(cell as! mycellview) }
i have thought kind of management handled, if not automatically arc, or uicollectionview
, @ least manually through preparecellforreuse
, apparently not.
hope helps :)
Comments
Post a Comment