Ios 在Xcode Swift上打开摄像头时应用程序崩溃
我是Swift的初学者,不知道它为什么会崩溃 模拟器很好(它只是给我一个信息,设备没有摄像头) 但当我在实际手机上打开它时,应用程序崩溃了。(将iphone x与11.2.6配合使用)Ios 在Xcode Swift上打开摄像头时应用程序崩溃,ios,iphone,swift,xcode,camera,Ios,Iphone,Swift,Xcode,Camera,我是Swift的初学者,不知道它为什么会崩溃 模拟器很好(它只是给我一个信息,设备没有摄像头) 但当我在实际手机上打开它时,应用程序崩溃了。(将iphone x与11.2.6配合使用) 除了相机功能外,其他功能都很好 我怎么修理它? 提前谢谢 import Foundation import UIKit class ProductInfoController: UIViewController, UIImagePickerControllerDelegate, UINavigationCon
除了相机功能外,其他功能都很好 我怎么修理它? 提前谢谢
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)
}
}
您必须在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
照片:
Key : Privacy - Camera Usage Description
Value : $(PRODUCT_NAME) camera use
Key : Privacy - Photo Library Usage Description
Value : $(PRODUCT_NAME) photo use
谢谢!问题解决如果问题解决了,你可以接受答案,这样对其他人会有帮助。我也有同样的问题,我被抑制了,日志中没有显示错误,只是崩溃。解决了,谢谢!