我是Swift的初学者,无法弄清楚它崩溃的原因。
模拟器很好(它只是给我一条消息,设备没有相机)
但是当我在实际手机上打开它时,应用程序崩溃了。 (使用iphone x和11.2.6)
其他所有功能都很好,但相机功能。
我该如何解决? 提前谢谢!
import Foundation
import UIKit
class ProductInfoController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate,
UITextFieldDelegate {
@IBOutlet weak var productName: UITextField!
@IBOutlet weak var productImage: UIImageView!
var product = Product()
var returningFromPicker = false
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
productName.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !returningFromPicker {
if productName.text == nil {
productName.text = product.productName
}
if productImage.image == nil {
productImage.image = product.productImage
}
}
}
@IBAction func pickImageFromCamera(_ sender: UIButton) {
chooseImage(fromLibrary: false)
}
@IBAction func pickImageFromLibrary(_ sender: UIButton) {
chooseImage(fromLibrary: true)
}
@IBAction func onNext(_ sender: UIButton) {
guard let name = productName.text else {
showAlert(message: "Provide a product name")
return
}
guard !name.isEmpty else {
showAlert(message: "Invalid product name provided")
return
}
guard product.productImage != nil else {
showAlert(message: "Provide a product photo")
return
}
product.productName = name
if let keywordsVC = storyboard?.instantiateViewController(withIdentifier: "keywordVC") as? KeywordsViewController {
keywordsVC.product = product
navigationController?.pushViewController(keywordsVC, animated: true)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
func chooseImage(fromLibrary: Bool) {
let sourceType:UIImagePickerControllerSourceType = fromLibrary ? .photoLibrary:.camera
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else {
showAlert(message: "Device doesn't have a " + (fromLibrary ? "Photo Library":"Camera"))
return
}
let imagePicker = UIImagePickerController();
imagePicker.delegate = self
imagePicker.allowsEditing = true
imagePicker.sourceType = sourceType
present(imagePicker, animated: true, completion: nil)
returningFromPicker = true
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
productImage.image = info[UIImagePickerControllerEditedImage] as? UIImage
product.productImage = productImage.image
picker.dismiss(animated: true, completion: nil)
returningFromPicker = false
}
}
extension UIViewController {
func showAlert(message: String) {
let alert = UIAlertController(title: "Title", message: message, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
}
答案 0 :(得分:4)
您必须在Info.plist中添加以下权限。 Info.plist中的权限
相机:
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
照片:
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use