美文网首页iOS Dev
如何播放webp图片

如何播放webp图片

作者: CoderZNB | 来源:发表于2017-03-31 15:09 被阅读86次

最近在做直播的礼物动画播放,其中就使用了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")
    }
}

相关文章

  • 如何播放webp图片

    最近在做直播的礼物动画播放,其中就使用了webp播放动画,我使用的是YYWebImage,首先我们这里还有一个要求...

  • iOS webp格式图片支持

    webp格式图片 webp格式图片是google推出的,相比jpg png有着巨大的优势,同样质量的图片webp格...

  • 资源图片优化

    tinypng 在线图片压缩 webP android studio选中图片右键Convert to webP

  • iOS-支持webp图片

    一. 本地webp图片+网络webp图片加载 安卓原生就已经支持webp图片了,毕竟是亲儿子啊,iOS可能因为是竞...

  • WebP图片的优势

    文章摘自本家网站 捷搜索 之 什么是WebP图片?什么是 WebP 格式图片?WebP 是由谷歌(google...

  • gulp-webp

    图片webp转换

  • iOS 加载WebP图片、WebP动图

    1、为什么要用WebP格式图片? 因为WebP格式图片是Google[https://baike.baidu.co...

  • Android 图片资源大瘦身

    Android 图片推荐使用WebP格式的图片 WebP格式,谷歌(google)开发的一种旨在加快图片加载速度的...

  • web 前端的一些知识

    webp 图片格式WebP格式,谷歌(google)开发的一种旨在加快图片加载速度的图片格式。图片压缩体积大约只有...

  • 自己开发WebP批量转化工具

    原文链接:自己开发WebP批量转化工具 由于WebP项目的启动,于是需要了解如何将png或jpg格式的图片转化为W...

网友评论

    本文标题:如何播放webp图片

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