2016-04-25 107 views
0

最近我发现有时候我的gif图片没有显示。所以我写一个测试代码,要在cellForRowAtIndexPath:方法用animatedImageWithImages创建的UIImage有错误?

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) 
      for i 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() 
     // Do any additional setup after loading the view, typically from a 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 the same UIImage object created by animatedImageWithImages 
       the image disappear when reuse, and when touching cell, it reappear. */ 
      imageView.image = self._gifImage 

      /** 2. create different UIImage by using same image array. this still not work */ 
//   imageView.image = UIImage.animatedImageWithImages(self._gifImageArray, duration: NSTimeInterval(self._gifImageArray.count) * 0.1) 

      /** 3. create different UIImage from data, this seems work 
       but use a lot of memories. and seems it has performance issue. */ 
//   imageView.image = UIImage.gifImage(self._gifData) 

      /** 4. use same image array as imageView's animationImages. 
       this kind works, but have different api than just set image. 
       and when click on cell, the animation stops. */ 
//   imageView.image = self._gifImageArray[0] 
//   imageView.animationImages = self._gifImageArray 
//   imageView.startAnimating() 
     } 

     return cell 
    } 
} 

通知,我尝试了不同的方式来设置ImageView的。

当我po对象在lldb,我注意到动画imageView有一个CAKeyframeAnimation。这是否提示?

我该如何让gif完美呈现?任何建议都会有所帮助。

这里是一个预览:经过滚动,GIF消失,重新出现在触摸

enter image description here

回答

1

我试过很多次之后,我终于想通了,我需要改变:

imageView.image = self._gifImage 

到:

imageView.image = nil // this is the magical! 
imageView.image = self._gifImage 

只是设置图像之前设置为新的图像,并动画工作!这就像一个神奇的!

这绝对是苹果的bug。

+0

真棒拯救我的生命,100 +信用@SolaWing – codercat

0

尝试通过与myImageview或其他名称替换imageView因为电池有默认imageView属性,因此,当你调用cell.imageView那么它返回单元格的默认imageview我认为。所以改变你的单元格的imageview的变量名称。希望这将帮助:)

+0

谢谢你的回复!但不幸的是,cell.imageView不是原因。实际上,在我的项目中,我使用了自定义ImageView。这里的cell.imageView只是一个方便。 – SolaWing

+0

我在说什么,如果您使用自定义的'ImageView'并将其出口名称作为'imageView',那么它可能会造成不明确性。我有这样的面子问题。我在自定义单元格中输出nae = me'imageView'的自定义图像视图,并且输出的内容与您发布的相关截图相同。这就是为什么我建议它 – Lion

+0

没有插座视图。这只是一个Xcode单一视图模板项目,所有代码都在这里发布。我也检查并尝试更换名称。名字不是原因。仍然感谢您的帮助! – SolaWing