简体   繁体   中英

App works fine in simulator but doesn't function correctly on phone

I am creating an OCR app in Swift for iOS and it requires pressing a button ('snap/upload image') which then allows the user to choose an image from their photos or to take a picture. This works perfectly in the iPhone 11 pro max simulator, however, when running on the iPhone 8 simulator and on the iPhone 8 itself, I get error messages or it just doesn't work. I have attached code and errors.

import UIKit
import MobileCoreServices
import TesseractOCR
import GPUImage

class ViewController: UIViewController {
  @IBOutlet weak var textView: UITextView!
  @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  // IBAction methods
  @IBAction func backgroundTapped(_ sender: Any) {
    view.endEditing(true)
  }

  @IBAction func takePhoto(_ sender: Any) {
    let imagePickerActionSheet =
      UIAlertController(title: "Snap/Upload Image",
                        message: nil,
                        preferredStyle: .actionSheet)

    if UIImagePickerController.isSourceTypeAvailable(.camera) {
      let cameraButton = UIAlertAction(
        title: "Take Photo",
        style: .default) { (alert) -> Void in
          self.activityIndicator.startAnimating()
          let imagePicker = UIImagePickerController()
          imagePicker.delegate = self
          imagePicker.sourceType = .camera
          imagePicker.mediaTypes = [kUTTypeImage as String]
          self.present(imagePicker, animated: true, completion: {
            self.activityIndicator.stopAnimating()
          })
      }
      imagePickerActionSheet.addAction(cameraButton)
    }

    let libraryButton = UIAlertAction(
      title: "Choose Existing",
      style: .default) { (alert) -> Void in
        self.activityIndicator.startAnimating()
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.mediaTypes = [kUTTypeImage as String]
        self.present(imagePicker, animated: true, completion: {
          self.activityIndicator.stopAnimating()
        })
    }
    imagePickerActionSheet.addAction(libraryButton)

    let cancelButton = UIAlertAction(title: "Cancel", style: .cancel)
    imagePickerActionSheet.addAction(cancelButton)

    present(imagePickerActionSheet, animated: true)
  }

  // Tesseract Image Recognition
  func performImageRecognition(_ image: UIImage) {
    let scaledImage = image.scaledImage(1000) ?? image
    let preprocessedImage = scaledImage.preprocessedImage() ?? scaledImage

    if let tesseract = G8Tesseract(language: "eng+fra") {
      tesseract.engineMode = .tesseractCubeCombined
      tesseract.pageSegmentationMode = .auto

      tesseract.image = preprocessedImage
      tesseract.recognize()
      textView.text = tesseract.recognizedText
    }
    activityIndicator.stopAnimating()
  }
}

// MARK: - UINavigationControllerDelegate
extension ViewController: UINavigationControllerDelegate {
}

// MARK: - UIImagePickerControllerDelegate
extension ViewController: UIImagePickerControllerDelegate {
  func imagePickerController(_ picker: UIImagePickerController,
       didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let selectedPhoto =
      info[.originalImage] as? UIImage else {
        dismiss(animated: true)
        return
    }
    activityIndicator.startAnimating()
    dismiss(animated: true) {
      self.performImageRecognition(selectedPhoto)
    }
  }
}

// MARK: - UIImage extension
extension UIImage {
  func scaledImage(_ maxDimension: CGFloat) -> UIImage? {
    var scaledSize = CGSize(width: maxDimension, height: maxDimension)

    if size.width > size.height {
      scaledSize.height = size.height / size.width * scaledSize.width
    } else {
      scaledSize.width = size.width / size.height * scaledSize.height
    }

    UIGraphicsBeginImageContext(scaledSize)
    draw(in: CGRect(origin: .zero, size: scaledSize))
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage
  }

  func preprocessedImage() -> UIImage? {
    let stillImageFilter = GPUImageAdaptiveThresholdFilter()
    stillImageFilter.blurRadiusInPixels = 15.0
    let filteredImage = stillImageFilter.image(byFilteringImage: self)
    return filteredImage
  }
}

[where the 'snap/upload image' is shown & its constraints][1]

When app is run on iphone 11 max pro simulator it works, however on the iphone 8 simulator, which is the device I have it makes this error

[Error][2]

In console:

2019-10-28 17:51:50.440498+0000 Love In A Snap[14813:703702] -[Love_In_A_Snap.ViewController takePhoto:]: unrecognized selector sent to instance 0x7fe503d06d50
2019-10-28 17:51:50.454363+0000 Love In A Snap[14813:703702] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Love_In_A_Snap.ViewController takePhoto:]: unrecognized selector sent to instance 0x7fe503d06d50'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e63d1ee __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x000000010c275b20 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e65e154 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKitCore                           0x0000000119832e79 -[UIResponder doesNotRecognizeSelector:] + 302
    4   CoreFoundation                      0x000000010e641f6c ___forwarding___ + 1436
    5   CoreFoundation                      0x000000010e6440f8 _CF_forwarding_prep_0 + 120
    6   UIKitCore                           0x0000000119806082 -[UIApplication sendAction:to:from:forEvent:] + 83
    7   UIKitCore                           0x00000001191ec8e5 -[UIControl sendAction:to:forEvent:] + 223
    8   UIKitCore                           0x00000001191ecc2f -[UIControl _sendActionsForEvents:withEvent:] + 398
    9   UIKitCore                           0x00000001191ebb8e -[UIControl touchesEnded:withEvent:] + 481
    10  UIKitCore                           0x0000000119840a31 -[UIWindow _sendTouchesForEvent:] + 2604
    11  UIKitCore                           0x0000000119842338 -[UIWindow sendEvent:] + 4596
    12  UIKitCore                           0x000000011981d693 -[UIApplication sendEvent:] + 356
    13  UIKitCore                           0x000000011989de5a __dispatchPreprocessedEventFromEventQueue + 6847
    14  UIKitCore                           0x00000001198a0920 __handleEventQueueInternal + 5980
    15  CoreFoundation                      0x000000010e5a0271 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x000000010e5a019c __CFRunLoopDoSource0 + 76
    17  CoreFoundation                      0x000000010e59f974 __CFRunLoopDoSources0 + 180
    18  CoreFoundation                      0x000000010e59a67f __CFRunLoopRun + 1263
    19  CoreFoundation                      0x000000010e599e66 CFRunLoopRunSpecific + 438
    20  GraphicsServices                    0x000000011055dbb0 GSEventRunModal + 65
    21  UIKitCore                           0x0000000119804dd0 UIApplicationMain + 1621
    22  Love In A Snap                      0x000000010a61132b main + 75
    23  libdyld.dylib                       0x000000010d132d29 start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

[Summary for future readers:]

This turned out to be an unrecognized selector crash; diagnosis was made more difficulty by the OP's failure to mention the crash or the crash log (the OP used only phrases like "nothing happens" or "can't press the button"). But we got there in the end.

However, it's a slightly unusual unrecognized selector , because the selector that Cocoa doesn't recognize is [Love_In_A_Snap.ViewController takePhoto:] , and we can see perfectly well that ViewController does have that method:

 @IBAction func takePhoto(_ sender: Any) {

In the end we had to resolve this by assuming that things had somehow become "stale". We deleted the connection in the storyboard and created it afresh. That sort of fixed things, but not in every case. Then we deleted the app from the device and cleaned the DerivedData folder, and that fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM