美文网首页iOS 知识库
Swift 4 Gif图的解析与使用

Swift 4 Gif图的解析与使用

作者: z小志 | 来源:发表于2018-04-27 17:52 被阅读38402次
        //分解gif图
        func resolveGifImage()->[UIImage]{
            var images:[UIImage] = []
            let gifPath = Bundle.main.path(forResource: "demo", ofType: "gif")
            if gifPath != nil{
                if let gifData = try? Data(contentsOf: URL.init(fileURLWithPath: gifPath!)){
                    let gifDataSource = CGImageSourceCreateWithData(gifData as CFData, nil)
                    let gifcount = CGImageSourceGetCount(gifDataSource!)
                    for i in 0...gifcount - 1{
                        let imageRef = CGImageSourceCreateImageAtIndex(gifDataSource!, i, nil)
                        let image = UIImage(cgImage: imageRef!)
                        images.append(image)
                    }
                }
            }
            return images
        }
    
    
        //使用
        override func viewDidLoad() {
            super.viewDidLoad()
        
        let imageView = UIImageView.init(frame: UIScreen.main.bounds)
        imageView.animationImages = resolveGifImage()
        imageView.animationDuration = 10
        imageView.animationRepeatCount = Int.max
        imageView.startAnimating()
    
        self.view.addSubview(imageView)
            
            // Do any additional setup after loading the view, typically from a nib.
        }
    
    

    相关文章

      网友评论

        本文标题:Swift 4 Gif图的解析与使用

        本文链接:https://www.haomeiwen.com/subject/vlrilftx.html