Swift 4.2 imagePickerController issue
The method signature has changed to
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
You should see a warning message for the func name
Instance method
'imagePickerController(:didFinishPickingMediaWithInfo:)' nearly
matches optional requirement
'imagePickerController(:didFinishPickingMediaWithInfo:)' of protocol
'UIImagePickerControllerDelegate'Candidate has non-matching type '(UIImagePickerController, [String :
Any]) -> ()'Move 'imagePickerController(_:didFinishPickingMediaWithInfo:)' to
another extension to silence this warningMake 'imagePickerController(_:didFinishPickingMediaWithInfo:)' private
to silence this warningRequirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)'
declared here (UIKit.UIImagePickerControllerDelegate)
imagePickerController - didFinishPickingMediaWithInfo does not get called after picking media
It appears as though the function declaration changed between Swift 3 and 4.2. This mustn't have been updated for you by the Swift Migrator Tool. One trick I do when this happens, to check what the correct function declaration is, is to use multiline comment syntax to comment out your current function (didFinishPickingMediaWithInfo
in your case). Then you can start typing the function out again, and use Xcode autocomplete to ensure you have it correct. You can then copy the contents of the function you commented out to this new and correct function declaration.
Or - you could just look it up the documentation! According to the documentation on imagePickerController, the function should be declared as:
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
If you replace your function declaration with the above, it should get called again.
UIImage?' is not convertible to 'UIImage' issue in swift 4
The UIImagePickerControllerDelegate
signature is different for different Swift versions. You are using the method of Swift 4.2 and later which is far different from Swift 4.0.
Swift 4.0:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
} else if let originalimage = info[UIImagePickerControllerOriginalImage] as? UIImage {
}
}
Swift 4.2 & Swift 5:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
} else if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
} else {
print("Something went wrong")
}
}
So, check the proper swift version and follow the proper methods!
UIImagePickerControllerDelegate didFinishPickingMediaWithInfo giving me an error
Use latest syntax
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let mediaType = info[.mediaType] as? String,
mediaType == (kUTTypeMovie as String),
let url = info[.mediaURL] as? URL
else { return }
dismiss(animated: true) {
let player = AVPlayer(url: url)
let vcPlayer = AVPlayerViewController()
vcPlayer.player = player
self.present(vcPlayer, animated: true, completion: nil)
}
}
}
InfoKey' is not a member type of 'UIImagePickerController'
You are attempting to use the newer iOS 12 API which requires Xcode 10. If you are using Xcode 9.4 then you are using iOS 11 and you need to use the soon to be older API.
private func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
}
UIImagePickerControllerOriginalImage is not working in ios 11.2.1
I ended up using this:
import Photos
extension UIImage {
static func from(info: [String : Any]) -> UIImage? {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
return image
}
var imageToBeReturned: UIImage?
if let url = info[UIImagePickerControllerReferenceURL] as? URL,
let asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil).firstObject {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: 1000, height: 1000), contentMode: .aspectFit, options: option, resultHandler: {(image: UIImage?, info: [AnyHashable : Any]?) in
imageToBeReturned = image
})
}
return imageToBeReturned
}
}
In this way-
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = UIImage.from(info: info) {
// I am happy :)
} else {
// I am sad :(
}
dismiss(animated: true, completion: nil)
}
This is working for me, please do suggest any improvements :)
Related Topics
Grab Frames from Video Using Swift
Pass C Function Callback in Swift
How to Capture Depth Data from Camera in iOS 11 and Swift 4
Checking If Textfields Are Empty Swift
Private Var Is Accessible from Outside the Class
"Cannot Assign Value of Type 'String' to Type 'Anyobject'", Swift 3, Xcode 8 Beta 6
Are Lazy Vars in Swift Computed More Than Once
How to Pause and Resume Nstimer.Scheduledtimerwithtimeinterval in Swift
How to Change the Associated Values of a Enum
How to Detect When Url of Amp Page Changed with Wkwebview
Reading Currently Playing Track in MACos Using Scriptingbridge Not Working
Can an Enum Contain Another Enum Values in Swift
Swift Error: Missing Return in a Function Expected to Return 'String'
For Loop Cycle Showing Always the Last Array Value Swift 3 After Http Request
Cannot Load Underlying Module for Xctest
Swiftui Hide Tabbar in Subview
How to Copy a Struct and Modify One of Its Properties at the Same Time