Async load image on swift / ios? -
here situation, ( please aware i'm starting on ios dev :) ).
i have list of feed data beign fetch firebase, each feed image , title in table view.
i'm looking way, if user come app without internet connection, still display data, whic have been display before that.
currently, i'm using display data:
import uikit import firebaseauth import firebasedatabase import firebasestorage import swiftkeychainwrapper import swiftyjson var posts = [post]() var selectedindexpath: int = 10 class feedvc: uiviewcontroller, uitableviewdatasource, uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate, uitableviewdelegate { @iboutlet weak var feedtableview: uitableview! private var readposts: observetask? override func viewdidload() { super.viewdidload() feedtableview.datasource = self feedtableview.delegate = self readposts = post.observelist(from: post.parentreference.queryordered(bychild: "privacy").queryequal(tovalue: false)) { observedposts in posts = observedposts.reversed() self.feedtableview.reloaddata() } } override var prefersstatusbarhidden: bool { return true } func numberofsections(in tableview: uitableview) -> int { return 1 } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return posts.count } func tableview(_ tableview: uitableview, heightforrowat indexpath: indexpath) -> cgfloat { return uitableviewautomaticdimension } func tableview(_ tableview: uitableview, estimatedheightforrowat indexpath: indexpath) -> cgfloat { return uitableviewautomaticdimension } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = self.feedtableview.dequeuereusablecell(withidentifier: "messagecell")! uitableviewcell let imageview = cell.viewwithtag(1) as! uiimageview let titlelabel = cell.viewwithtag(2) as! uilabel let linklabel = cell.viewwithtag(3) as! uilabel titlelabel.text = posts[indexpath.row].title titlelabel.numberoflines = 0 linklabel.text = posts[indexpath.row].link linklabel.numberoflines = 0 storage.getimage(with: posts[indexpath.row].imageurl){ postpic in imageview.image = postpic } return cell } func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { guard posts[indexpath.row].title != "coming soon" else { return } selectedindexpath = indexpath.row self.performsegue(withidentifier: "push", sender: self) self.feedtableview.reloaddata() } }
how possible ? i've been looking nsur function, start ?
thanks lot time !
use core data store images or
if don't want store these images in core data, since can impact performance if data set grows large. better write images files.
nsdata *pngdata = uiimagepngrepresentation(image);
this pulls out png data of image you've captured. here, can write file:
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentspath = [paths objectatindex:0]; //get docs directory nsstring *filepath = [documentspath stringbyappendingpathcomponent:@"image.png"]; //add file name [pngdata writetofile:filepath atomically:yes]; //write file
reading later works same way. build path did above, then:
nsdata *pngdata = [nsdata datawithcontentsoffile:filepath]; uiimage *image = [uiimage imagewithdata:pngdata];
what you'll want make method creates path strings you, since don't want code littered everywhere. might this:
- (nsstring *)documentspathforfilename:(nsstring *)name { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); nsstring *documentspath = [paths objectatindex:0]; return [documentspath stringbyappendingpathcomponent:name]; }
hope that's helpful.
Comments
Post a Comment