【问题标题】:Pick Video And Pics from photo gallary please请从照片库中选择视频和图片
【发布时间】:2019-01-04 01:44:06
【问题描述】:

我正在拼命尝试将视频选择选项添加到我的应用中。该应用程序可以拍照并选择图片,但不能选择视频文件。我希望用户能够像选择照片一样轻松地从照片库中选择视频。但我问过的大多数人似乎认为这是不可能的。但当然不是。我已经使用了许多具有此功能的应用程序。或者这不是我可以用 Xcode 做的事情?

  }
    let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
    actionsheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction) in
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let mypickerController = UIImagePickerController()
            mypickerController.delegate = self
            mypickerController.sourceType = .photoLibrary
            self.present(mypickerController, animated: true, completion: nil)
        }
    }))
    actionsheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction) in
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self
            myPickerController.sourceType = .camera
            self.present(myPickerController, animated: true, completion: nil)
        }
    }))
    actionsheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(actionsheet, animated: true, completion: nil)
}

@IBAction func onSaveButton(_ sender: Any) {
    print("screenshot pressed")

    //let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
    //appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()

    if firstImage.image == nil{
        GlobalFunction.sharedManager.setWhiteBackground(selectView: firstImage)
    }
    if secondImage.image == nil{
        GlobalFunction.sharedManager.setWhiteBackground(selectView: secondImage)
    }
    if thirdImage.image == nil{
        GlobalFunction.sharedManager.setWhiteBackground(selectView: thirdImage)
    }
    let image = GlobalFunction.sharedManager.takeScreenshot(theView: backView)//takeScreenshot(theView: backView)
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}

@IBAction func onCancelButton(_ sender: Any) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
    self.present(vc, animated: true, completion: nil)
}

@IBAction func onAddButton(_ sender: Any) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
    self.present(vc, animated: true, completion: nil)
}
@objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo: UnsafeRawPointer) {
    DispatchQueue.main.async{

        self.showAlertView(message: "Your story has been saved to your Camera Roll", completionHandler: { (a) in

            let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
            appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()

        })

        //UIAlertView(title: "Success", message: "Your story has been saved to your Camera Roll", delegate: nil, cancelButtonTitle: "Close").show()
    }
}

func takeScreenshot(theView: UIView) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(theView.bounds.size, true, 0.0)
    theView.drawHierarchy(in: theView.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}

func circleCropDidCancel() {
    dismiss(animated: false, completion: nil)
}

func circleCropDidCropImage(_ image: UIImage) {
    dismiss(animated: false, completion: nil)
    if selected == 1{
        firstImage.image = image
    }else if selected == 2{
        secondImage.image = image
    }else if selected == 3{
        thirdImage.image = image
    }

}

}

【问题讨论】:

标签: ios iphone video uiimagepickercontroller


【解决方案1】:

UIImagePickerController 有一个mediaTypes property。它决定了选择器可以选择什么样的媒体。您没有将 mypickerController.mediaTypes 设置为包含视频。

【讨论】:

  • 我确实在 Apple 文档中找到了以下代码。 “要指定电影捕获接口,或指示在浏览保存的媒体时只显示电影,请在如下语句中使用 kUTTypeMovie 标识符:myImagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil ]; 要为源指定所有可用的媒体类型,请使用如下语句:myImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera]; "
  • 我的代码是用 If, let 语句编写的...我用这个替换哪个代码 ----: myImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
猜你喜欢
  • 1970-01-01
  • 2014-04-23
  • 2020-10-02
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多