iOS开发之PHLivePhotoView

2023-12-05 13:56:33 浏览数 (2)

介绍

  • PHLivePhotoView 也是一个 UIView,专门用于显示 Live Photo。
  • Live Photo 本质是一张 HEIC 格式的封面图片 一段 MOV 格式的视频。
  • 默认情况下,Live Photo 只显示封面图片,需要长按才能播放视频。
  • 通过 PHLivePhotoViewDelegate 代理可以监听 Live Photo 的开始播放与结束播放。

案例

代码语言:javascript复制
import PhotosUI
import UIKit

class ViewController: UIViewController {
    // PHLivePhotoView
    lazy var livePhotoView: PHLivePhotoView = {
        let livePhotoView = PHLivePhotoView(frame: CGRect(x: 0,
                                                          y: 200,
                                                          width: UIScreen.main.bounds.width,
                                                          height: 300))
        livePhotoView.contentMode = .scaleAspectFit
        // 播放时是否静音
        livePhotoView.isMuted = false
        livePhotoView.delegate = self
        return livePhotoView
    }()

    // PHPickerViewController
    lazy var pickerViewController: PHPickerViewController = {
        var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
        configuration.filter = PHPickerFilter.any(of: [.images, .livePhotos])
        var pickerViewController = PHPickerViewController(configuration: configuration)
        pickerViewController.delegate = self
        return pickerViewController
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(livePhotoView)
    }

    // PHLivePhotoView播放视频
    @IBAction func playButtonClicked(_ sender: Any) {
        livePhotoView.startPlayback(with: .full)
    }

    // 访问相册
    @IBAction func photoButtonClicked(_ sender: Any) {
        present(pickerViewController, animated: true, completion: nil)
    }
}

// MARK: - PHPickerViewControllerDelegate
extension ViewController: PHPickerViewControllerDelegate {
    // 从相册取出LivePhoto显示到PHLivePhotoView
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        picker.dismiss(animated: true, completion: nil)
        for result in results {
            let itemProvider = result.itemProvider
            if itemProvider.canLoadObject(ofClass: PHLivePhoto.self) {
                itemProvider.loadObject(ofClass: PHLivePhoto.self) { object, error in
                    guard error == nil else { return }
                    guard let livePhoto = object as? PHLivePhoto else { return }
                    DispatchQueue.main.async {
                        self.livePhotoView.livePhoto = livePhoto
                    }
                }
            }
        }
    }
}

extension ViewController: PHLivePhotoViewDelegate {
    // 能否播放
    func livePhotoView(_ livePhotoView: PHLivePhotoView,
                       canBeginPlaybackWith playbackStyle: PHLivePhotoViewPlaybackStyle) -> Bool {
        print(#function)
        return true
    }

    // 开始播放
    func livePhotoView(_ livePhotoView: PHLivePhotoView,
                       willBeginPlaybackWith playbackStyle: PHLivePhotoViewPlaybackStyle) {
        print(#function)
    }

    // 播放结束
    func livePhotoView(_ livePhotoView: PHLivePhotoView, didEndPlaybackWith playbackStyle: PHLivePhotoViewPlaybackStyle) {
        print(#function)
    }
}

效果

播放Live Photo.gif

0 人点赞