ios - Tableview scroll too slow loading images -


my issue in app load images in table view. when scroll through table view slow , stops scrolling sometimes. looked online , saw using dispatch help. tried code below , app crashes saying nil.

code:

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0)) {                  let parseimage: pffile = objectsong.valueforkey("picture") pffile                 var pic: uiimage = uiimage(data: parseimage.getdata())!                      dispatch_async(dispatch_get_main_queue()) {                         cell.imageview.image = pic   <-- crash happens here                     }                 }             } 

previous code used load table view cells:

        let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) tableviewcell         let objectsong: pfobject = object pfobject          cell.songimage.image = uiimage(data: parseimage.getdata()) 

that approach cannot work.

remember how tableview reusable cells works. same cell used different rows. if download image asynchronously , try store in cell, cell might used different row. , anyway, set image on background thread, , going cause trouble.

here's do: cache images. when cell loaded, cached image. if there set it, if not there set placeholder image , start download.

when download finished, cannot put image cell. instead, add cache. figure out row should display image (in case rows have been added or removed), reload row on main thread. reload pick image cache.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -