swift3播放webp

这篇博客主要介绍了在Swift3中如何使用YYAnimatedImageView来播放WebP格式的图片,并通过添加ATS设置以及引入YYImage/WebP库来支持WebP。同时,对于GIF图片,提出了利用KVO监听currentAnimatedImageIndex属性来实现仅播放一次的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  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开始就让动画停止

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值