我今天开始使用Apple's Official Guide for Swift Development开始学习iOS开发。
我已到达Work with View Controllers。在页面结尾之前有一个检查点,我应该在模拟器上运行应用程序,当我单击图像视图时,我应该得到SIGABRT
错误,因为我没有显示图像的权限
不仅我没有收到任何错误消息,单击图像视图时根本没有任何反应。
我已经检查过3次文件,以确保我没有错过任何内容。我甚至创建了一个新项目,以确保一切正常,但仍然无法正常工作。
ViewController.swift:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
//MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Handle the text field's user input through delegate callbacks.
nameTextField.delegate = self
}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
mealNameLabel.text = textField.text
}
//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
//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, completion: nil)
}
@IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
}
你能帮我解决一下吗?
答案 0 :(得分:0)
当您尝试在未经许可的情况下访问相机胶卷时,应该会发生此错误。
iOS 10要求您添加关于您想要访问相机胶卷的原因的说明。这需要使用密钥Privacy - Photo Library Usage Description
如果您没有收到此错误,我猜您已经在我们在旧版iOS上运行的plist文件中输入了此信息?或者访问相机胶卷的代码实际上没有触发,您可能需要点击屏幕或按下按钮来启动代码。
答案 1 :(得分:0)
我正在使用Tap Gesture Recognizer,我使用Storyboard放置在UIImage上。
为了实现这一点,我还必须在故事板中UIImage的属性检查器中标记为“启用用户交互”的块中放置一个复选标记。