HHK1 / PryntTrimmerView

A set of tools to trim, crop and select frames inside a video

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using ThumbSelectorView in SwiftUI

cuhte3 opened this issue · comments

commented

Thanks for the great library! Am trying to use ThumbSelectorView in SwiftUI with UIViewRepresentable, but got the "could not calculate thumb size error". Looking for the past issues am assume i need to set ThumbSelectorView asset into viewDidAppear when the view rect is calculated, however there is no the kind of option is UIViewRepresentable lifecycle. Any advice how to use the ThumbSelectorView in SwiftUI?

example:

struct VideoCoverImageSelectorView: UIViewRepresentable  {
  @ObservedObject var videoViewModel: VideoViewModel

  func makeUIView(context: Context) -> UIView {
    let thumbSelector = ThumbSelectorView()
    videoViewModel.stopped = true
    thumbSelector.delegate = videoViewModel
    thumbSelector.asset = AVAsset(url: videoViewModel.url)
    return thumbSelector
  }
  
  func updateUIView(_ uiView: UIView, context: Context) {}
}

I don't have time right now to test it, but if you find a solution I think it would be great to add an example into the Readme.

Try nesting a ThumbSelectorView inside a UIView.

struct VideoCoverImageSelectorView: UIViewRepresentable  {
    let asset: AVAsset?
    var getThumbSelectorView: ((ThumbSelectorView) -> Void)?
    
    func makeUIView(context: Context) -> FrameView {
        let thumbSelectorFrameView = FrameView()
        getThumbSelectorView?(thumbSelectorFrameView.thumbSelectorView)
        thumbSelectorFrameView.thumbSelectorView.asset = asset
        return thumbSelectorFrameView
    }
    
    func updateUIView(_ uiView: FrameView, context: Context) {
        uiView.thumbSelectorView.asset = asset
    }
    
    class FrameView: UIView {
        let thumbSelectorView  = ThumbSelectorView()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            addSubview(thumbSelectorView)
            let constraints = [
                thumbSelectorView.centerXAnchor.constraint(equalTo: centerXAnchor),
                thumbSelectorView.centerYAnchor.constraint(equalTo: centerYAnchor),
                thumbSelectorView.widthAnchor.constraint(equalTo: widthAnchor),
                thumbSelectorView.heightAnchor.constraint(equalTo: heightAnchor),
            ]
            NSLayoutConstraint.activate(constraints)
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}