swift3播放webp

//
//  ViewController.swift
//  yywebimagetest
//
//  Created by targetcloud on 2017/3/29.
//  Copyright © 2017年 targetcloud. All rights reserved.
//

import UIKit
import YYWebImage

class ViewController: UIViewController {

    @IBOutlet weak var imageView: YYAnimatedImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        YYImageWebPAvailable() ? print("支持") : print("不支持")
        imageView.yy_imageURL = URL(string: "http://file4.qf.56.itc.cn/style/static/gift/m/v2/webp/menghuanhunli.webp")
        imageView.addObserver(self, forKeyPath: "currentAnimatedImageIndex", options: [.new], context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        let newValue = change![NSKeyValueChangeKey.newKey] as! UInt
        newValue == 0 ? imageView.stopAnimating() : ()
    }
    
}

注意:

1、imageView類型改爲YYAnimatedImageView(sb、XIB)

2、加ATS

3、cocoapod上加入  pod 'YYImage/WebP',完整的podfile如下

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'yywebimagetest' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for yywebimagetest
pod 'YYImage/WebP'
pod 'YYWebImage'
end

4、YYImageWebPAvailable() ? print("支持") : print("不支持") 用來先判斷一下是否支持webp

5、若要GIF只播放一次用KVO方式實現,跟蹤屬性currentAnimatedImageIndex,第一次播放從1開始,接下來重複播放時從0開始,所以第二次從0開始就讓動畫停止

發佈了131 篇原創文章 · 獲贊 14 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章