【问题标题】:Inserting a Video from the Photo Library in Xcode在 Xcode 中插入照片库中的视频
【发布时间】:2020-10-05 00:26:49
【问题描述】:

我已经设置了从照片库中插入图像,现在我正试图让用户也从照片库或 iOS 中的相机胶卷中选择一个视频。我写了以下代码:-

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBAction func didTapOnImageView(sender: UITapGestureRecognizer) {
    
    //call Alert function
    self.showAlert()
    
}

//Show alert to selected the media source type.
private func showAlert() {
    
    var alertStyle = UIAlertController.Style.actionSheet
    
    if (UIDevice.current.userInterfaceIdiom == .pad) {
      alertStyle = UIAlertController.Style.alert
    }

    let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: alertStyle)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
        self.getImage(fromSourceType: .camera)
    }))
    alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
        self.getImage(fromSourceType: .photoLibrary)
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

//get image from source type
private func getImage(fromSourceType sourceType: UIImagePickerController.SourceType) {

    //Check is source type available
    if UIImagePickerController.isSourceTypeAvailable(sourceType) {

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = sourceType
        imagePickerController.mediaTypes = ["public.image", "public.movie"]
        self.present(imagePickerController, animated: true, completion: nil)
    }
}

//MARK:- UIImagePickerViewDelegate.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    self.dismiss(animated: true) { [weak self] in

        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
        //Setting image to your image view
        
        self?.imageView.image = image
        self?.imageView.contentMode = .scaleToFill
        self?.image20 = self?.imageView.image
            
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

}

它非常适合选择图像并显示它,但是,现在我可以选择视频但不能显示它。谁能帮助我在 imagePickerController 委托中包含什么来获取视频 URL 并显示它?谢谢您的帮助!欣赏!

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    您绝对可以从imagePicker 中获取视频数据,如下所示:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
            picker.dismiss(animated: true) {
                if let image = info[.originalImage] as? UIImage,
                    let data = image.jpegData(compressionQuality: 0.8) {
                }
            }
            
            if let videoURL = info[.mediaURL] as? URL {
                do {
                    let videoData = try Data(contentsOf: videoURL, options: .mappedIfSafe)
                } catch {
                    print("Error: \(error.localizedDescription)")
                }
            }
        }
    

    现在,在获取您选择的视频文件的Data 之后,下一步就是研究在视图中播放视频数据。你可以从这里开始:Implementing video view in the Storyboard

    但是要注意那个链接的年代,它是从 2015 年开始的,但它应该仍然可以帮助你。

    【讨论】:

      【解决方案2】:
      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
          videoURL = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerReferenceURL")] as? URL
      

      }

      获取videoURL后可以传给AVPlayer播放。

      let player = AVPlayer(url: videoURL)
      let playerViewController = AVPlayerViewController()
      playerViewController.player = player
      present(playerViewController, animated: true) {
      playerViewController.player!.play()
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        相关资源
        最近更新 更多