1

我希望能够循环播放实时照片,以进行连续播放。

到目前为止,我正在尝试使用PHLivePhotoViewDelegate来完成此操作。

import Foundation
import SwiftUI
import PhotosUI
import iOSShared

struct LiveImageView: UIViewRepresentable {
    let view: PHLivePhotoView
    let model:LiveImageViewModel?
    let delegate = LiveImageLargeMediaDelegate()
    
    init(fileGroupUUID: UUID) {
        let view = PHLivePhotoView()
        // Without this, in landscape mode, I don't get proper scaling of the image.
        view.contentMode = .scaleAspectFit
        
        self.view = view
        
        // Using this to replay live image repeatedly.
        view.delegate = delegate
        
        model = LiveImageViewModel(fileGroupUUID: fileGroupUUID)
        
        guard let model = model else {
            return
        }
        
        model.getLivePhoto(previewImage: nil) { livePhoto in
            view.livePhoto = livePhoto
        }
    }
    
    func makeUIView(context: Context) -> UIView {
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        guard let model = model else {
            return
        }
        
        guard !model.started else {
            return
        }
        
        model.started = true
        view.startPlayback(with: .full)
    }
}

class LiveImageLargeMediaDelegate: NSObject, PHLivePhotoViewDelegate {
    func livePhotoView(_ livePhotoView: PHLivePhotoView, didEndPlaybackWith playbackStyle: PHLivePhotoViewPlaybackStyle) {
        livePhotoView.stopPlayback()
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) {
            livePhotoView.startPlayback(with: .full)
        }
    }
}

但没有完全成功。似乎音频确实再次播放,但不是视频。livePhotoView.stopPlayback和异步方面只是我正在尝试的其他更改。我也试过没有这些。

请注意,我不希望用户必须手动更改实时照片(例如,请参阅NSPredicate 以不包括 Loop and Bounce Live Photos)。

想法?

4

1 回答 1

0

ChrisPrince我尝试了您的代码,它对我来说运行良好,我只需添加委托并在其中开始播放,一切都运行良好且顺利。我认为使用停止播放没有意义,因为该函数本身表示播放结束。

func livePhotoView(_ livePhotoView: PHLivePhotoView, didEndPlaybackWith playbackStyle: PHLivePhotoViewPlaybackStyle) {
        livePhotoView.startPlayback(with: .full)
}
于 2021-12-10T10:23:19.833 回答