didFinishPickingImage won't perform segue - ios

I'm trying to perform a segue after I've used the UIImagePickerController, but the didFinishPickingImage function won't perform it, and I honestly have no idea why.
My code simply looks like this:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: nil)
self.performSegueWithIdentifier("fromidtomain", sender: self)
}
There's also some saving of the image involved but I'm guessing that's not relevant.
Any suggestions on how to do this properly would be appreciated.
EDIT: It works now, but only if I put the segue-code inside an if statement in the function.

Implement the UIImagePickerControllerDelegate delegate in your class.
class GameOver: UIViewController, UIImagePickerControllerDelegate {
...
}

Related

Importing image from photo library into an app in xcode

My app keeps crashing and I don't know why. I'm working on a tabbed application part by part and testing it every time I get a part of it done.
Right now I'm working on trying to import an image from the user's device but I can't seem to get it.
I'm currently using Xcode 10.2.1 and I understand that there has been some changes to the delegate methods and I have changed them. It succeeds in building but whenever I tap that one particular tab where I would like to import an image, it crashes.
class UserImage: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePickerController : UIImagePickerController!
#IBOutlet var ImageView: UIImageView!
#IBAction func Edit(_ sender: Any) {
imagePickerController.delegate = self
imagePickerController.sourceType = .photoLibrary
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
if let image = info[.originalImage] as? UIImage {
ImageView.image = image
} else {
print("Take another")
}
self.dismiss(animated: true, completion: nil)
}
It is crashing because you never actually initialized the UIImagePickerController. Since you marked the imagePickerController as a non optional value, your app is going to crash when you try to reference it but it is still nil. Add this line to the beginning of your Edit function to initialize the variable:
imagePickerController = UIImagePickerController()
Edit: Alternatively, as #rmaddy mentioned, you could just make the controller a local variable of the function. In your example, there is no need to make it a property of the class. Essentially you would just remove the declaration from the top of your class and instead declare it inside the function:
let imagePickerController = UIImagePickerController()

Issue with Swift code to make recognition app

**hello everyone ... I am trying to make an app recognition, simply I have an image view, text view and button and code is below .. my first issue is that my imageview isnt changing after picking a picture! .. my second issue that it tells me that "Arguments label image1 do not match any available overloads "
any help pls ?
import UIKit
import CoreImage
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
#IBOutlet var myimage: UIImageView!
#IBOutlet var info: UITextView!
#IBAction func Import(_ sender: Any) {
// create image picker
let imagepicker = UIImagePickerController()
imagepicker.delegate=self
imagepicker.sourceType=UIImagePickerControllerSourceType.photoLibrary
self.present(imagepicker ,animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let Image1 = info[UIImagePickerControllerOriginalImage] as? UIImage {
myimage.image = Image1
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
// detect function
func detect(){
let image2 = CIImage (Image1:myimage.image!)!
// issue is here it says
"Arguments label image1 do not match any available overloads "
}
}
First issue: Looks like your delegate method declaration doesn't match the one in the protocol, so it's treated as a separate method. Xcode even gives a warning:
Instance method 'imagePickerController(picker:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'
Try changing this line:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
to:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
UPD: please also read #dfd's comments below on how to avoid this (quite a common) type of confusion.
Second issue: as already mentioned by others, you should pass the parameter as the initialiser properly. With minimum changes to your code it will be:
let image2 = CIImage (image: myimage.image!)!
However, the less exclamation marks (i.e. force unwrapping) the better; for more info on that, check Optional Chaining as an Alternative to Forced Unwrapping.
This error because you have write error label
CIImage.init(image: <#T##UIImage#>)
you can do it like this
guard let image = myimage.image else {
return
}
let image2 = CIImage.init(image: image)
Your first problem is that you are missing the _ in your delegate call, just before picker: and you need to write Any instead of AnyObject. It should be:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { //... }
Second problem, your function needs to be written like this:
let image2 = CIImage(image: myimage.image!)

Swift Selector in UIImageWriteToSavedPhotosAlbum not recognized, why?

iOS API Function UIImageWriteToSavedPhotosAlbum takes a selector as one argment:
func UIImageWriteToSavedPhotosAlbum(_ image: UIImage,
_ completionTarget: Any?,
_ completionSelector: Selector?,
_ contextInfo: UnsafeMutableRawPointer?)
https://developer.apple.com/documentation/uikit/1619125-uiimagewritetosavedphotosalbum
However, in swift, when I call this function, the selector never gets recognized:
class Base {
func save_image(img:UIImage) {
UIImageWriteToSavedPhotosAlbum(img, self, Selector("image:didFinishSavingWithError:contextInfo:"), nil)
// I also tried this:
// UIImageWriteToSavedPhotosAlbum(img, self, #selector(image(_:didFinishSavingWithError:contextInfo:))
}
#objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
print("Photo Saved Successfully")
}
}
class Child:Base {
}
// This is how I call the save_image function:
let child = Child()
child.save_image()
As you can see, I tried constructing the selector from the signature, and from a string, but neither works. I always get this error in runtime:
'XXX.Child' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector ......
What is happening here? I am wondering if this is because swift doesn't see the method from the Child class, as the method is inherited from Base class?
How can I pass the selector successfully?
Relevant question I have read:
#selector() in Swift?
methodSignatureForSelector is the NSObject's method.
So, You need to inherit the NSObject class.
class Base: NSObject {
...
}
Provide some guidance to your selector to help it find the right function:
class Base {
func save_image(img:UIImage) {
UIImageWriteToSavedPhotosAlbum(img, self, #selector(Base.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
#objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
print("Photo Saved Successfully")
}
}
class Child:Base {
}
// This is how I call the save_image function:
let child = Child()
child.save_image()
After all I have tried, I find that the crucial things are:
method signature must match(a lot of answers have covered that)
do not pass the parameters to the wrong positions, one of my failures is, I passed the target to the contextInfo
maybe in Swift only, the class that implements the the callback function, must be inherited from NSObject

Type does not conform to protocol 'ImagePickerDelegate'

I'm using a library called ImagePicker from github to get images from the user and upload them. However when i implement the sample code, found here, i get the following error:
Type 'changeCoverViewController' does not conform to protocol 'ImagePickerDelegate'
on line one of the code:
class changeCoverViewController: UIViewController, ImagePickerDelegate {
You are probably missing one of the required ImagePickerDelegate functions. Specifically these:
func wrapperDidPress(imagePicker: ImagePickerController, images: [UIImage])
func doneButtonDidPress(imagePicker: ImagePickerController, images: [UIImage])
func cancelButtonDidPress(imagePicker: ImagePickerController)
In the future if you notice this error again go to the declaration of the delegate and you'll see a class protocol and that'll tell you what is required to implement said delegate. In this case:
public protocol ImagePickerDelegate: class {
func wrapperDidPress(imagePicker: ImagePickerController, images: [UIImage])
func doneButtonDidPress(imagePicker: ImagePickerController, images: [UIImage])
func cancelButtonDidPress(imagePicker: ImagePickerController)
}

imagePickerController:didFinishPickingMediaWithInfo conflicts with optional requirement method in protocol 'UIImagePickerControllerDelegate'

Here is the full error:
Objective-C method 'imagePickerController:didFinishPickingMediaWithInfo:' provided by method
'imagePickerController(_ :didFinishPickingMediaWithInfo:)'
conflicts with optional requirement method 'imagePickerController(_:didFinishPickingMediaWithInfo:)'
in protocol 'UIImagePickerControllerDelegate'
It occurs on the first of this function in my ViewController.swift file:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageView.contentMode = .ScaleAspectFit
ImageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
I am trying to follow this tutorial: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/
from the error method, I gather that there is a method didFinishPickingMediaWithInfo that it is getting from the imagePickerController base class and it doesn't like that I'm trying to overwrite it. But thats all I know. All of the imagePickerController functions I find online look like this. What am I doing wrong?
I am using Xcode 7, if that makes a difference. ]
The correct function head is:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
<#code#>
}
Note the String instead of NSObject in the declaration of the info dictionary.
I am not sure to why the docs are saying that you have to write NSObject, but String is the correct one.
If you implement any protocol methods I would recommend using the auto completion of Xcode to make sure that you don't run into problems like this.
I am unsure where Xcode gets that auto completion from but it appears to always be in sync with the actual compiler which in the end is the one thing you have to worry about rather than some online apple docs! Especially in times when the frameworks are constantly changing and even the language itself is under development.
If you're using Xcode 7, you may be targeting iOS 9, then, in which the method signature changed slightly from:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
to:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
The NSObject has changed to String.
In Swift 3 it changed to
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
Below is format of didFinishPickingMediaWithInfo delegate of UIImagePickerController:
public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
In your case use String instead of NSObject in function parameter.
I had this issue writing an extension to UIImagePickerControllerDelegate in XCode 8
XCode 8 Autocomplete produces a method signature with an underscore for the first parameter. This creates a segmentation fault in the Swift compiler. Removing the underscore fixes the issue.
// CRASH SegFault 11
#objc protocol MyDelegate: UIImagePickerControllerDelegate {
}
extension MyDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// code
}
}
// DOESN'T CRASH
#objc protocol MyDelegate: UIImagePickerControllerDelegate {
}
extension MyDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// code
}
}

Resources