**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!)
Related
I am using PHPickerViewController to pick Image for User Profile Picture Purpose in iOS 15. I am using UIKit framework. I have the following code:
var pickerConfig = PHPickerConfiguration(photoLibrary: .shared())
pickerConfig.selectionLimit = 1
pickerConfig.filter = .images
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)
The Picker is working properly for selecting images and delegating the results. But, when the Cancel button is pressed, nothing happens and the Picker is not dismissed as expected.
How to dismiss the PHPickerViewController instance when its own Cancel button is pressed ?
Edit:
The implementation of PHPickerViewControllerDelegate Method is as follows:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
{
results.first?.itemProvider.loadObject(ofClass: UIImage.self) { [unowned self] reading , error in
guard let image = reading as? UIImage, error == nil else
{
DispatchQueue.main.async {
picker.dismiss(animated: true)
self.profilePictureHasError = true
self.toggleDoneButtonEnabled()
}
return
}
self.profilePictureHasError = false
DispatchQueue.main.async {
picker.dismiss(animated: true)
self.profilePictureHasChanged = self.userProfilePicture != image
if self.profilePictureHasChanged
{
self.profilePictureView.image = image
self.toggleDoneButtonEnabled()
}
}
}
}
You need to dismiss the picker yourself in the picker(_:didFinishPicking:) delegate method which is called when the user completes a selection or when they tap the cancel button.
From the Apple docs for picker(_:didFinishPicking:):
The system doesn’t automatically dismiss the picker after calling this method.
For example:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// Do something with the results here
picker.dismiss(animated: true)
}
Your current delegate code only calls picker.dismiss when the results array is non-empty (i.e when the user has selected images). When the cancel button is tapped, the delegate method is called with an empty results array.
Fix the issue by adding the following to the top of the code in your delegate method:
if results.isEmpty {
picker.dismiss(animated: true)
return
}
you just wrap it out in an objc func for making cancel button works
#objc
func didOpenPhotos() {
lazy var pickerConfig = PHPickerConfiguration()
pickerConfig.filter = .images
pickerConfig.selectionLimit = 1
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)
}
call it anywhere
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()
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
}
}
I'm a newbie with Ios. i'm learning swift and overlooked object c.
Currently, i'm writing an demo with swift and xcode 6.1 which can scan qrcode and barcode from camera or an image from image library.
I know that AVFoundation framework support scanning qrcode and barcode, but it can only scan from camera.
I searched and found zbarSDK which support scan code from camera and image. I do as instructions in http://zbar.sourceforge.net/iphone/sdkdoc/ and NSFastEnumeration in Swift (convert code to swift). However, when i run app, after choosing image from library, it happen error.
This's my code
import UIKit
import AVFoundation
extension ZBarSymbolSet: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
class FirstViewController: UIViewController, ZBarReaderDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let reader = ZBarReaderController()
#IBOutlet weak var lblResult: UILabel!
#IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
reader.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func scanCode(sender: AnyObject) {
let scanner = reader.scanner
scanner.setSymbology(ZBAR_I25, config: ZBAR_CFG_ENABLE, to: 0)
reader.modalPresentationStyle = .Popover
presentViewController(reader, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var results: NSFastEnumeration = info["ZBarReaderControllerResults"] as NSFastEnumeration
var symbolFound : ZBarSymbol?
// =============== Error here ==================
for symbol in results as ZBarSymbolSet {
symbolFound = symbol as? ZBarSymbol
break
}
var resultString = NSString(string: symbolFound!.data)
println(resultString)
}
}
here is error image
I will very grateful if someone let me know why it happen error and how to fix it or there's any way to scan code with an image using AVFoundation or there a other library (with detail document and sample) to do this (please give detail instructions because i have just learned swift and ios for 3 weeks).
Thanks.
I was also looking to read a QR code from an image and without Zbar.
You can use CIDetector instead. I found the solution here.
In my case, I pick up a photo from the library (supposed to be a QR code, here qrcodeImg) and then convert it in CIImage to be decoded by CIDetector.
qrCodeImageView.image=qrcodeImg
let detector:CIDetector=CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
let ciImage:CIImage=CIImage(image:qrcodeImg)
var qrCodeLink=""
let features=detector.featuresInImage(ciImage)
for feature in features as! [CIQRCodeFeature] {
qrCodeLink += feature.messageString
}
if qrCodeLink=="" {
print("nothing")
}else{
print("message: \(qrCodeLink)")
}
For Swift3 following code should work for you to get result from ZBarSDK
extension ZBarSymbolSet: Sequence {
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// ADD: get the decode results
let results: NSFastEnumeration = info[ZBarReaderControllerResults] as! NSFastEnumeration
var symbolFound : ZBarSymbol?
for symbol in results as! ZBarSymbolSet {
symbolFound = symbol as? ZBarSymbol
break
}
let resultString = symbolFound!.data
print(resultString)
}
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 {
...
}