如何播放webp图片

最近在做直播的礼物动画播放,其中就使用了webp播放动画,我使用的是YYWebImage,首先我们这里还有一个要求就是只播放一次,我发现YYWebImage的API中并没有只播放一次的方法,这里我使用KVO实现动画只播放一次

Demo

  1. 将 cocoapods 更新至最新版本.
  2. 在 Podfile 中添加 pod 'YYWebImage'。
  3. 执行 pod install 或 pod update。
  4. 导入 <YYWebImage/YYWebImage.h>。
  5. 注意:pod 配置并没有包含 WebP 组件, 如果你需要支持 WebP,可以在 Podfile 中添加 pod 'YYImage/WebP'。你可以调用 YYImageWebPAvailable() 来检查一下 WebP 组件是否被正确安装。

用法:从URL 加载图片

// 加载网络图片
imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/logo.png"];

// 加载本地图片
imageView.yy_imageURL = [NSURL fileURLWithPath:@"/tmp/logo.png"];

加载动图

// 只需要把 `UIImageView` 替换为 `YYAnimatedImageView` 即可。
UIImageView *imageView = [YYAnimatedImageView new];
imageView.yy_imageURL = [NSURL URLWithString:@"http://github.com/ani.webp"];

渐进式图片加载

// 渐进式:边下载边显示
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];

// 渐进式加载,增加模糊效果和渐变动画 
[imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

只播放一次

import UIKit
import YYWebImage
class ViewController: UIViewController {

    @IBOutlet weak var imageView: YYAnimatedImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if YYImageWebPAvailable() { // 来检查一下 WebP 组件是否被正确安装
            print("支持")
        }else {
            print("不支持")
        }
        imageView.yy_imageURL = URL(string: "http://file4.qf.56.itc.cn/style/static/gift/m/v2/webp/menghuanhunli.webp")
        // imageView.currentAnimatedImageIndex
        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
        if newValue == 0 {
            imageView.stopAnimating()
        }
        
    }
    deinit {
        removeObserver(self, forKeyPath: "currentAnimatedImageIndex")
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 8天20亿,再破30亿,《战狼Ⅱ》用国产炮火打出了一个票房奇迹。这些天来,人民日报、新华社、中国青年报等一系列官方...
    RMwang阅读 4,278评论 0 5
  • 【奇谈杂阅】管理学大师彼得德鲁克晚年给自己定义身份的时候,并没把自己定义为管理学家,而是叫社会生态学家。因为在多年...
    奇妙的奇阅读 1,007评论 0 0
  • “滴滴滴,滴滴滴”随着闹钟铃声响起,新的一天就开始了!咦?女主呢?(⊙o⊙)哦,还在睡懒觉呢! “哎哟!”...
    70cf67a8e318阅读 3,690评论 0 0