ios - Swift - Adding image to tableview cell from asset library -
i trying add image tableview cell not having luck getting display.
the image being loaded file system (i println()
result) uiimage cannot seem cell. placing println()
after closure shows me images loaded after cell has been returned.
here code:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell") as! uitableviewcell let note = notes[indexpath.row] let nsurl = nsurl(string: note.valueforkey("url") as! string)! var loaderror: nserror? var image: uiimage? assetslibrary!.assetforurl(nsurl, resultblock: { (asset) -> void in if let ast = asset { let iref = ast.defaultrepresentation().fullresolutionimage().takeunretainedvalue() image = uiimage(cgimage: iref) println("the loaded image: \(image)") } }, failureblock: {(error) -> void in loaderror = error }) cell.textlabel!.text = note.valueforkey("title") as? string cell.imageview!.image = image return cell }
when replace closure following load image project shows in table. leads me believe not due issue way story board set up.
uiimage(named: transportitems[indexpath.row])
any appreciated.
it doesn't work way. cellforrowatpathindex synchronous routine. assetforurl asynchronous routine. return data long time after cellforrowatpathindex has returned.
here's should do: have method cachedassetforurl returns asset immediately, or returns nil. if returns asset, store it. remember has efficient possible, because called while user scrolls , down through images.
if method returns nil, trigger download in background. when download finishes, don't try store image in cell - time, same cell display entirely different object! instead store data cachedassetforurl able return asset, , invalidate row of table view.
Comments
Post a Comment