禁用纵向导入视频

时间:2018-02-25 19:38:13

标签: ios swift uiimagepickercontroller

我们有一个简单的视频应用,我们不允许用户以肖像拍摄。我们允许导入在应用外拍摄的视频,但如果可能,我们会阻止导入以纵向拍摄的视频。非常感谢任何帮助!

  @IBAction func importVideoTapped(_ sender: Any) {

    imagePickerController.sourceType = .savedPhotosAlbum
    imagePickerController.delegate = self
    imagePickerController.mediaTypes = [kUTTypeMovie as String]
    present(imagePickerController, animated: true, completion: nil)

}

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    viewController.navigationItem.title = "Videos"
}

1 个答案:

答案 0 :(得分:0)

据我所知,您无法过滤选择器中显示的视频,因此只有风景视频可供选择。因此,您必须让用户选择视频,检查其是否为风景,否则会显示提醒。以下内容:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    guard
        let url = info[UIImagePickerControllerMediaURL] as? URL,
        let videoTrack = AVAsset(url: url).tracks(withMediaType: AVMediaType.video).first else
    {
        dismiss(animated: true, completion: nil)
        return
    }

    let size = videoTrack.naturalSize
    let transform = videoTrack.preferredTransform

    //Check if video is in landscape orientation (left or right)
    if (size.width == transform.tx && size.height == transform.ty) || transform.tx == 0 && transform.ty == 0 {
        //TODO: store video however you want for later use
        dismiss(animated: true, completion: nil)
    } else {
        let alert = UIAlertController(title: "Invalid video", message: "Please select a landscape video", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
        picker.show(alert, sender: nil)
    }
}

确保以适合您的任何方式调整实施,并正确存储视频或其网址。