ios - UIImage created with animatedImageWithImages have bugs? -
recently found gif image not shown. write test code it:
import uikit import imageio extension uiimage { static func gifimagearray(data: nsdata) -> [uiimage] { let source = cgimagesourcecreatewithdata(data, nil)! let count = cgimagesourcegetcount(source) if count <= 1 { return [uiimage(data: data)!] } else { var images = [uiimage](); images.reservecapacity(count) in 0..<count { let image = cgimagesourcecreateimageatindex(source, i, nil)! images.append( uiimage(cgimage: image) ) } return images; } } static func gifimage(data: nsdata) -> uiimage? { let gif = gifimagearray(data) if gif.count <= 1 { return gif[0] } else { return uiimage.animatedimagewithimages(gif, duration: nstimeinterval(gif.count) * 0.1) } } } class viewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate { let _gifdata : nsdata = nsdata(contentsofurl: nsurl(string: "https://upload.wikimedia.org/wikipedia/commons/d/d3/newtons_cradle_animation_book_2.gif")!)! lazy var _gifimagearray : [uiimage] = uiimage.gifimagearray(self._gifdata) lazy var _gifimage : uiimage = uiimage.gifimage(self._gifdata)! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. let table = uitableview(frame: self.view.bounds, style: .plain) table.datasource = self table.delegate = self self.view.addsubview(table) } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return 1000; } func numberofsectionsintableview(tableview: uitableview) -> int { return 1; } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let identifier = "cell" var cell : uitableviewcell! = tableview.dequeuereusablecellwithidentifier(identifier); if cell == nil { cell = uitableviewcell(style: .default, reuseidentifier: identifier) } if let imageview = cell.imageview { /** 1. use same uiimage object created animatedimagewithimages image disappear when reuse, , when touching cell, reappear. */ imageview.image = self._gifimage /** 2. create different uiimage using same image array. still not work */ // imageview.image = uiimage.animatedimagewithimages(self._gifimagearray, duration: nstimeinterval(self._gifimagearray.count) * 0.1) /** 3. create different uiimage data, seems work use lot of memories. , seems has performance issue. */ // imageview.image = uiimage.gifimage(self._gifdata) /** 4. use same image array imageview's animationimages. kind works, have different api set image. , when click on cell, animation stops. */ // imageview.image = self._gifimagearray[0] // imageview.animationimages = self._gifimagearray // imageview.startanimating() } return cell } }
notice in cellforrowatindexpath:
method, tried different way set imageview.
when po object in lldb, notice animated imageview have cakeyframeanimation. makes tip?
how can make gif shown perfectly? suggestion helpful.
here preview: after scroll, gif disappear, , reappear when touch
after tried many times, have figured out need change:
imageview.image = self._gifimage
to:
imageview.image = nil // magical! imageview.image = self._gifimage
just set image nil before set new image, , animation work!! it's magical!
this apple's bug.
Comments
Post a Comment