gpt4 book ai didi

ios - 类型 'UIImagePickerControllerSourceType' 没有成员 'photoLibrary'

转载 作者:行者123 更新时间:2023-11-28 06:28:05 25 4
gpt4 key购买 nike

这是代码:

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

这是错误:类型“UIImagePickerControllerSourceType”没有成员“photoLibrary”

只是想完成官方教程 Start Developing iOS Apps (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"
}

最佳答案

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

imagePickerController.sourceType = .PhotoLibrary

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

编辑:

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

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

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

    NSCameraUsageDescription用于拍摄照片效果的新图像NSPhotoLibrary使用说明用于选择照片效果的图像

您可以在标签中放置不同的值。

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]
}
}
}

关于ios - 类型 'UIImagePickerControllerSourceType' 没有成员 'photoLibrary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com