Swift 3如何限制用户只拍摄照片而不是视频 - iOS,Xcode

时间:2017-06-29 00:38:49

标签: swift xcode swift3 xcode8 uiimagepickercontroller

当用户选择视频时,我的照片应用会崩溃。用户可以选择照片并且工作正常,但我想删除拍摄视频的选项。如何禁用视频?相关的代码块低于......

@IBAction func choosePhoto(_ sender:UIButton){

    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
    imagePicker = UIImagePickerController()
    imagePicker.delegate = self

    if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
        let cameraAction = UIAlertAction(title: "Use Camera", style: .default) { (action) in

            let status = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)

            if (status == .authorized) {
                self.displayPicker(type: .camera)
            }

            if (status == .restricted) {

                self.handleRestricted()
            }

            if (status == .denied) {

                self.handleDenied()
            }

            if (status == .notDetermined) {

                AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted) in
                    if (granted) {
                        self.displayPicker(type: .camera)
                    }

                })
            }

        }

    alertController.addAction(cameraAction)
    }

    if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
        let photoLibraryAction = UIAlertAction(title: "Use Photo Library", style: .default) { (action) in

            let status = PHPhotoLibrary.authorizationStatus()

            if (status == .authorized) {
                self.displayPicker(type: .photoLibrary)
            }

            if (status == .restricted) {

                self.handleRestricted()
            }

            if (status == .denied) {

                self.handleDenied()
            }

            if (status == .notDetermined) {

                PHPhotoLibrary.requestAuthorization({ (status) in
                    if (status == PHAuthorizationStatus.authorized) {
                        self.displayPicker(type: .photoLibrary)
                        }
                    })

                }


            }

        alertController.addAction(photoLibraryAction)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertController.addAction(cancelAction)

    present(alertController, animated: true, completion: nil)
}

func handleDenied() {

    let alertController = UIAlertController(title: "Camera Access Denied", message: "This app does not have acces to your device's camera. Please update your settings.", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Go To Settings", style: .default) { (action) in
        DispatchQueue.main.async {
            UIApplication.shared.open(NSURL(string: UIApplicationOpenSettingsURLString)! as URL)
        }
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alertController.popoverPresentationController?.sourceView = self.photoImage
    alertController.popoverPresentationController?.sourceRect = self.photoImage.bounds

    alertController.addAction(settingsAction)
    alertController.addAction(cancelAction)
    present(alertController, animated: true, completion: nil)
}

func handleRestricted() {

    let alertController = UIAlertController(title: "Camera Access Denied", message: "This device is restricted from accessing the camera at this time", preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)

    alertController.popoverPresentationController?.sourceView = self.photoImage
    alertController.popoverPresentationController?.sourceRect = self.photoImage.bounds

    alertController.addAction(defaultAction)
    present(alertController, animated: true, completion: nil)

}

func displayPicker(type: UIImagePickerControllerSourceType) {
    self.imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: type)!
    self.imagePicker.sourceType = type
    self.imagePicker.allowsEditing = true

    DispatchQueue.main.async {
        self.present(self.imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
    photoImage.contentMode = .scaleAspectFill
    photoImage.image = chosenImage
    dismiss(animated: true, completion: nil)
}

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

2 个答案:

答案 0 :(得分:0)

你想做的是在你初始化imagePicker之后设置以下属性

imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

https://developer.apple.com/documentation/uikit/uiimagepickercontroller/1619173-mediatypes

答案 1 :(得分:0)

将此行放在imagePicker.delegate = self

之后
imagePicker.mediaTypes = [kUTTypeImage as String] 

请记住在文件的开头添加import MobileCoreServices