请从照片库中选择视频和图片

时间:2018-07-27 01:00:42

标签: ios iphone video uiimagepickercontroller

我正拼命尝试将视频选择选项添加到我的应用中。该应用程序可以拍照并选择图片,但不能选择视频文件。我希望用户能够像选择照片一样轻松地从照片库中选择视频。但是我问过的大多数人似乎都认为不可能。但是,当然不是。我已经使用了许多具有此功能的应用程序。还是这不是我可以用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
    }

}

}

1 个答案:

答案 0 :(得分:0)

UIImagePickerController具有一个mediaTypes property。它确定选择器可以选择哪种媒体。您没有设置mypickerController.mediaTypes包括视频。