Reputation: 780
I upgraded project to Swift 3 and I am getting a crash on the line (at various points/viewControllers in the project) :
present(picker, animated: true, completion: nil)
in the function:
@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)
}
The crash message in the console:
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
in my in info.plist I have:
<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>
I looked around and did see that having NSPhotoLibraryUsageDescription and NSCameraUsageDescription in info.plist solves the issue (UIImagePickerController in Swift 3 and UIImagePickerController crashes app | Swift3, Xcode8), but it doesn't for me. I did clean and build, restarted xCode, restarted Mac, etc.
It might be worth pointing out that my console at startup says:
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.
but according to this thread that doesn't matter https://forums.developer.apple.com/thread/63254
my class has the delegates:
class PersonalSettingsVC: UIViewController, UITextFieldDelegate, UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
Any help is much appreciated
Upvotes: 2
Views: 3775
Reputation: 2005
Try to add NSCameraUsageDescription
key to your PrettyApp-Info.plist file with some text description.
Upvotes: 0
Reputation: 869
for me it worked after I added the following in info.plist Privacy - Camera Usage Description $(PRODUCT_NAME) camera use
Upvotes: 1
Reputation: 780
Ok, so I figured it out. (3 days of my life) Even though I put the Privacy keys in my info.plist, they were not actually accessed/ recognised by the app. so I had to go top level app -> Targets -> "MY_APP" -> info and put them there as well. after that, it ran. (Solutions from Jecky, Seggy and KAR were great, so I upvoted). obviously a bug, hope it helps someone.
Upvotes: 2
Reputation: 1196
Try this for 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
}
}
Upvotes: 0
Reputation: 3361
try this for your swift 3.0 code,
@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 })
}
It will present picker in your app and will work perfect.
Hope it will solve your problem.
Upvotes: 0
Reputation: 2394
Try My code
It is with take picture with camera or choose from library.
Just give UIImagePickerControllerDelegate,UINavigationControllerDelegate
Set Property in Info.plist file Privacy - Photo Library Usage Description : Allow photo
@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)
}
Upvotes: 0