ios - How can I download Images off the web and load it in a UICollectionView then pass an array of images to the next view? -
so parsing json custom objects. inside these objects have url string make asynchronous call grab these images. load collection view.
my problems are:
1) how can load these images on cells without them disappearing or appearing on different cells scroll?
2) when tap on cell has pass on view , show on imageview
3) after image shows must have array of images in previous collection view switching amongst each other in same order.
please help!
also no third party libraries please!
import uikit class homepagecollectionviewcontroller: uicollectionviewcontroller { var imagecache = nscache() var hingeimagesarray = [hingeimage]() var arraytoholdconvertedurltouiimages = [uiimage]() var task: nsurlsessiondatatask? //full of urls var hingeimageurls = [string]() var testimagearray = [uiimage]() override func viewdidload() { super.viewdidload() // makes network call hingeimages refreshitems() } override func numberofsectionsincollectionview(collectionview: uicollectionview) -> int { return 1 } override func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return hingeimagesarray.count } override func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier("imagereusecell", forindexpath: indexpath) as! imagecollectionviewcell let image = hingeimagesarray[indexpath.row] if let imageurl = image.imageurl { if let url = nsurl(string: imageurl) { //check cached images , if found set them cells - works if images go off screen if let myimage = imagecache.objectforkey(image.imageurl!) as? uiimage { cell.collectionviewimage.image = myimage print(myimage) }else { // request images asynchronously collection view not slow down/lag self.task = nsurlsession.sharedsession().datataskwithurl(url, completionhandler: { (data, response, error) -> void in // check if there data returned guard let data = data else { return } // create image object our data , assign cell if let hingeimage = uiimage(data: data){ self.testimagearray.append(hingeimage) //cache images/set key self.imagecache.setobject(hingeimage, forkey: image.imageurl!) dispatch_async(dispatch_get_main_queue(), { () -> void in cell.collectionviewimage.image = hingeimage }) } }) task?.resume() } } } return cell } override func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) { if let cell = collectionview.cellforitematindexpath(indexpath) { performseguewithidentifier("nextview", sender: cell) }else{ print("no good") } } override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if let indexpath = self.collectionview?.indexpathforcell(sender as! imagecollectionviewcell) { if segue.identifier == "nextview" { let galleryviewcontroller = segue.destinationviewcontroller as! galleryviewcontroller //we passing url next screen making request show them let imageurl = hingeimagesarray[indexpath.row] //passesd single image url galleryviewcontroller.imageurlfromotherscreen = imageurl.imageurl //pasing array of urls galleryviewcontroller.arrayofimageurlsfromotherview = hingeimageurls //let myindex = testimagearray[indexpath.row] //let image = arraytoholdconvertedurltouiimages[indexpath.row] //galleryviewcontroller.selectedimagefrompreviousscreen = image // galleryviewcontroller.arrayofimageobjects = arraytoholdconvertedurltouiimages //print(arraytoholdconvertedurltouiimages) } } } //execute json request func refreshitems() { hingeclient.executejsonnetworkrequest { (hingeimages, error) in guard let images = hingeimages else { print("error getting images json") return } //loop through array , append urls new array moreimages in hingeimages! { self.hingeimageurls.append(moreimages.imageurl!) } self.hingeimagesarray = images self.collectionview?.reloaddata() } } }
//more code illustrate how setting imageview.
func moretest() { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) { urls in self.arrayofimageurlsfromotherview { if let url = nsurl(string: urls) { if let data = nsdata(contentsofurl: url) { if let hingeimage = uiimage(data: data) { self.convertedurltoimages.append(hingeimage) dispatch_async(dispatch_get_main_queue(), { if self.galleryimageview.image == nil { self.galleryimageview.image = self.convertedurltoimages[0] }else{ print("not empty") } }) } } } } } }
Comments
Post a Comment