ios - SWIFT 2 - UICollectionView - slow scrolling -
i have setup uicollectionview in project data json file. works however, scrolling slow , when view scrolling coming cell, few moments shows content of cell before.
i have tried using dispatch_async
still slow , jumpy.
any idea doing wrong?
override func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let videocell = collectionview.dequeuereusablecellwithreuseidentifier("videocell", forindexpath: indexpath) uicollectionviewcell let communityviewcontroller = storyboard?.instantiateviewcontrollerwithidentifier("community_id") videocell.frame.size.width = (communityviewcontroller?.view.frame.size.width)! videocell.center.x = (communityviewcontroller?.view.center.x)! videocell.layer.bordercolor = uicolor.lightgraycolor().cgcolor videocell.layer.borderwidth = 2 let fileurl = nsurl(string:self.uservideosinfo[indexpath.row][2]) let asset = avasset(url: fileurl!) let assetimggenerate = avassetimagegenerator(asset: asset) assetimggenerate.appliespreferredtracktransform = true let time = cmtimemake(asset.duration.value / 3, asset.duration.timescale) dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) { //self.showindicator() let namelabelstring = self.uservideosinfo[indexpath.row][0] let commentlabelstring = self.uservideosinfo[indexpath.row][1] let datelabelstring = self.uservideosinfo[indexpath.row][3] let buttonplayuservideo = videocell.viewwithtag(1) as! uibutton let namelabel = videocell.viewwithtag(2) as! uilabel let commentuservideo = videocell.viewwithtag(3) as! uilabel let dateuservideo = videocell.viewwithtag(4) as! uilabel let thumbuservideo = videocell.viewwithtag(5) as! uiimageview let deleteuservideo = videocell.viewwithtag(6) as! uibutton buttonplayuservideo.layer.setvalue(indexpath.row, forkey: "indexplaybtn") deleteuservideo.layer.setvalue(indexpath.row, forkey: "indexdeletebtn") dispatch_async(dispatch_get_main_queue()) { namelabel.text = namelabelstring commentuservideo.text = commentlabelstring dateuservideo.text = datelabelstring self.shadowtext(namelabel) self.shadowtext(commentuservideo) self.shadowtext(dateuservideo) if let cgimage = try? assetimggenerate.copycgimageattime(time, actualtime: nil) { thumbuservideo.image = uiimage(cgimage: cgimage) } } } //this important videocell.layer.shouldrasterize = true videocell.layer.rasterizationscale = uiscreen.mainscreen().scale return videocell }
at first - working ui objects global queue , seems without purpose. forbidden - or behavior undefined.
secondary, heavy operation creation of thumbnail perform on main queue. consider using of avassetimagegenerator
's method
public func generatecgimagesasynchronouslyfortimes(requestedtimes: [nsvalue], completionhandler handler: avassetimagegeneratorcompletionhandler)
instead of own asyncs.
at third, viewwithtag
pretty heavy operation causing enumeration on subviews
. consider declare properties in cell views need.
upd: declare properties in cell, create subclass of uicollectionviewcell
appropriate properties iboutlets. then, in view controller viewdidload
implementation, call
collecionview.registerclass(<yourcellsubclass>.dynamictype, forcellwithreuseidentifier:"videocell")
or, if collection view cell configured in storyboard, specify class of cell , connect subviews class' outlets directly in cell's settings window in interface builder.
at fourth, cells being reused collection view. each time cell going out of visible area, removed collection view , put reuse queue. when scroll cell, view controller asked again provide cell. , you're fetching thumbnail video again each newly appeared cell. consider caching of fetched thumbnails storing them in array collectionview's indexpath.item
index.
Comments
Post a Comment