[英]iOS Swift 3 UIImagePickerController crash
我將項目升級到Swift 3,並且在行中崩潰(在項目中的各個點/ viewControllers處):
present(picker, animated: true, completion: nil)
在函數中:
@IBAction func userImage(_ sender: AnyObject){
print("button pressed")
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = false
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil) // -> crashes here
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageToPost.setImage(newImage, for: UIControlState())
self.imageToPost.imageView?.contentMode = UIViewContentMode.scaleAspectFill
picker.dismiss(animated: true, completion: nil)
}
控制台中的崩潰消息:
libsystem_kernel.dylib`__abort_with_payload:
0x11108d138 <+0>: movl $0x2000209, %eax ; imm = 0x2000209
0x11108d13d <+5>: movq %rcx, %r10
0x11108d140 <+8>: syscall
-> 0x11108d142 <+10>: jae 0x11108d14c ; <+20>
0x11108d144 <+12>: movq %rax, %rdi
0x11108d147 <+15>: jmp 0x111086d6f ; cerror_nocancel
0x11108d14c <+20>: retq
0x11108d14d <+21>: nop
0x11108d14e <+22>: nop
0x11108d14f <+23>: nop
在我的info.plist中,我有:
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo gallery use</string>
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use </string>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) microphone use</string>
我環顧四周,確實發現info.plist中包含NSPhotoLibraryUsageDescription和NSCameraUsageDescription解決了該問題( Swift 3中的 UIImagePickerController和UIImagePickerController使app | Swift3,Xcode8崩潰 ),但這並不適合我。 我做了清理和構建,重新啟動了xCode,重新啟動了Mac等。
可能值得指出的是,我的控制台在啟動時說:
objc[11204]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11a0d8910) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x119f02210). One of the two will be used. Which one is undefined.
但是根據這個線程沒關系https://forums.developer.apple.com/thread/63254
我班有代表:
class PersonalSettingsVC: UIViewController, UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
任何幫助深表感謝
好的,我知道了。 (生命中的3天)即使我將隱私密鑰放在了info.plist中,該應用程序實際上也並未對其進行訪問/識別。 因此,我必須轉到頂級應用程序->目標->“ MY_APP”->信息,並將其放置在該位置。 之后,它運行了。 (來自Jecky,Seggy和KAR的解決方案很棒,因此我投票贊成)。 顯然是一個錯誤,希望它能對某人有所幫助。
對我來說,在我在info.plist中添加以下內容后,它起作用了-相機使用說明$(PRODUCT_NAME)相機使用
試試我的代碼
用相機拍照或從圖庫中選擇。
只需給UIImagePickerControllerDelegate,UINavigationControllerDelegate
在Info.plist文件中設置屬性隱私-照片庫使用說明:允許照片
@IBAction func dsfsd(_ sender: AnyObject) {
let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Swiftly Now! Choose an option!", preferredStyle: .actionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .default) { action -> Void in
if( UIImagePickerController.isSourceTypeAvailable(.camera))
{
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .camera
self.present(myPickerController, animated: true, completion: nil)
}
else
{
let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel) { action -> Void in
//Just dismiss the action sheet
}
actionController.addAction(cancelAction)
self.present(actionController, animated: true, completion: nil)
}
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .default) { action -> Void in
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.present(actionSheetController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imgview.image = image
self.dismiss(animated: true, completion: nil)
}
試試Swift 3。
@IBAction func userImage(_ sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
self.imagePicker.delegate = self
self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
self.imagePicker.allowsEditing = true
self.present(self.imagePicker, animated: true, completion: nil)
self.imagePicked.isUserInteractionEnabled = true
}
}
嘗試此操作以獲取快速的3.0代碼,
@IBAction func takePhoto(_ sender: UIButton) {
var picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .camera
self.present(picker, animated: true, completion: { _ in })
}
@IBAction func selectPhoto(_ sender: UIButton) {
var picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: { _ in })
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [AnyHashable: Any]) {
var chosenImage = info[UIImagePickerControllerEditedImage]
self.imageView!.image = chosenImage
picker.dismiss(animated: true, completion: { _ in })
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: { _ in })
}
它將在您的應用中顯示選擇器,並且可以完美運行。
希望它能解決您的問題。
嘗試使用一些文本說明將NSCameraUsageDescription
鍵添加到PrettyApp-Info.plist文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.