输入' UIImagePickerControllerSourceType'没有会员' photoLibrary'

时间:2016-12-18 13:54:53

标签: ios swift xcode

这是代码:

 // Only allow photos to be picked,not taken.
    imagePickerController.sourceType = .photoLibrary

这是错误:输入' UIImagePickerControllerSourceType'没有会员' photoLibrary'

试图完成官方教程开始开发iOS应用程序(Swift)

//MARK: Actions
@IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {

    //Hide the keyboard.
    nameTextField.resignFirstResponder()

    // UIImagepickerController is a view controller that lets a user pick media from their photo library.
    let imagePickerController = UIImagePickerController()

    // Only allow photos to be picked,not taken.
    imagePickerController.sourceType = .photoLibrary

    //Make sure ViewController is notified when the user picks an image.
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, competion: nil)
}
@IBAction func setDefaultLabelText(sender: UIButton) {
    mealNameLabel.text = "Default Text"
}

}

1 个答案:

答案 0 :(得分:1)

好吧,看起来你可能正在使用Swift 2.2(也许这也适用于Swift 2.3)。你想要这种语法:

imagePickerController.sourceType = .PhotoLibrary

注意大小写。请记住,Xcode 8.2(2016年12月12日发布)将是Xcode的最后一个版本,以支持Swift 2.x. 2017年的某个时候,您可能需要使用Swift 3来提交新的App Store。

编辑:

这是我的完整Swift 2.2代码,后面是我的Swift 3.0代码。请注意两件事:

  • 我也在使用相机应用程序,并进行检查以确保它存在 - 这不是模拟器的情况。

  • 除非您将以下内容添加到info.plist文件中,否则您的应用在iOS 10中崩溃:

    NSCameraUsageDescription 用于捕捉照片效果的新图像 NSPhotoLibraryUsageDescription 用于选择照片效果的图像

您可以在标签中添加不同的值。

Swift 2.2:

let picker = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self
}

extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    // MARK: Camera App

    func openCameraApp() {
        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            picker.modalPresentationStyle = .FullScreen
            presentViewController(picker,
                                  animated: true,
                                  completion: nil)
        } else {
            noCamera()
        }
    }
    func noCamera(){
        let alertVC = UIAlertController(
            title: "No Camera",
            message: "Sorry, this device has no camera",
            preferredStyle: .Alert)
        let okAction = UIAlertAction(
            title: "OK",
            style:.Default,
            handler: nil)
        alertVC.addAction(okAction)
        presentViewController(
            alertVC,
            animated: true,
            completion: nil)
    }

    // MARK: Photos Albums

    func showImagePicker() {
        picker.allowsEditing = false
        picker.sourceType = .PhotoLibrary
        //        picker.modalPresentationStyle = .Popover
        presentViewController(picker,
                              animated: true,
                              completion: nil)
        picker.popoverPresentationController?.sourceView = self.view
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        image = chosenImage
        self.performSegueWithIdentifier("ShowEditView", sender: self)
        dismissViewControllerAnimated(true, completion: nil)
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(false, completion: nil)
    }

    // MARK: Seque to EditViewController

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowEditView" {
            if let vc = segue.destinationViewController as? EditViewController {
                vc.image = image
            }
        }
    }
}

Swift 3.0(只有扩展代码有语法更改):

func openCameraApp() {
    if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,
                animated: true,
                completion: nil)
    } else {
        noCamera()
    }
}
func noCamera(){
    let alertVC = UIAlertController(
        title: "No Camera",
        message: "Sorry, this device has no camera",
        preferredStyle: .alert)
    let okAction = UIAlertAction(
        title: "OK",
        style:.default,
        handler: nil)
    alertVC.addAction(okAction)
    present(
        alertVC,
        animated: true,
        completion: nil)
}

// MARK: Photos Albums

func showImagePicker() {
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    //        picker.modalPresentationStyle = .Popover
    present(picker,
            animated: true,
            completion: nil)
    picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    image = chosenImage
    self.performSegue(withIdentifier: "ShowEditView", sender: self)
    dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: false, completion: nil)
}

// MARK: Seque to EditViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowEditView" {
        if let vc = segue.destination as? EditViewController {
            vc.image = image
            //                vc.image = images[0]
        }
    }
}