美文网首页
【GIF图片的播放】

【GIF图片的播放】

作者: 大基本功 | 来源:发表于2018-06-02 17:11 被阅读9次

具体实现步骤

1.加载gif图片,并且转成Data类型
2.从data中读取数据:将data转成CGImageSource对象
3.获取所有图片
4.播放图片组

import UIKit
import ImageIO

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.加载Gif图片, 并且转成Data类型
        guard let path = Bundle.main.path(forResource: "demo.gif", ofType: nil) else { return }
        guard let data = NSData(contentsOfFile: path) else { return }
        
        // 2.从data中读取数据: 将data转成CGImageSource对象
        guard let imageSource = CGImageSourceCreateWithData(data, nil) else { return }
        let imageCount = CGImageSourceGetCount(imageSource)
        
        // 3.便利所有的图片
        var images = [UIImage]()
        var totalDuration : TimeInterval = 0
        for i in 0..<imageCount {
            // 3.1.取出图片
            guard let cgImage = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
            let image = UIImage(cgImage: cgImage)
            if i == 0 {
                imageView.image = image
            }
            images.append(image)
            
            // 3.2.取出持续的时间
            guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, i, nil) as? NSDictionary else { continue }
            guard let gifDict = properties[kCGImagePropertyGIFDictionary] as? NSDictionary else { continue }
            guard let frameDuration = gifDict[kCGImagePropertyGIFDelayTime] as? NSNumber else { continue }
            totalDuration += frameDuration.doubleValue
        }
        
        // 4.设置imageView的属性
        imageView.animationImages = images
        imageView.animationDuration = totalDuration
        imageView.animationRepeatCount = 0
        
        // 5.开始播放
        // imageView.startAnimating()
    }
}

相关文章

  • GIF图片的播放和生成

    首先需要导入头文件 GIF图片的播放 GIF图片的生成

  • iOS-播放gif动画文件(OC方法)

    iOS-.gif动画文件的播放 前言 播放gif动画的方法有多种: 将gif图片分解成多张图片使用UIImageV...

  • iOS 播放 gif 动画

    使用UIWebView播放 将GIF图片分解成多张PNG图片,使用UIImageView播放

  • gif 图片播放

    使用 UIWebView 播放 使用 UIImageView + SDWebImage 自定义 MBProgres...

  • 播放Gif图片

    // 1.加载Gif图片, 并且转成Data类型 guard let path = Bundle.main.pat...

  • 【GIF图片的播放】

    具体实现步骤 1.加载gif图片,并且转成Data类型2.从data中读取数据:将data转成CGImageSou...

  • iOS swift 手动实现播放GIF图片

    GIF图片本身就是一帧帧的图片,想要手动实现GIF图片的播放,需要将GIF图片里边的每一帧图片获取到,然后利用UI...

  • iOS-GIF图播放

    封装播放GIF图片的Imageview分类GIF图片来源为本地或网络导入系统库: import ImageIO 加...

  • iOS中展示gif图片(swift)

    需要导入框架: 实现步骤: 获取gif图片 获取组成gif图片的总图片数量值 通过遍历,获取单张图片,及其播放时间...

  • ImageMagick之制作gif图片

    ImageMagick之制作gif图片 gif动画由一系列图片按照一定的时间间隔来播放的,每张单独的图片作为gif...

网友评论

      本文标题:【GIF图片的播放】

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