簡體   English   中英

應用程序在模擬器中運行良好,但 function 在手機上無法正常運行

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

我正在 Swift 中為 iOS 創建一個 OCR 應用程序,它需要按下一個按鈕(“快照/上傳圖像”),然后允許用戶從他們的照片中選擇圖像或拍照。 這在 iPhone 11 pro max 模擬器中完美運行,但是,在 iPhone 8 模擬器和 iPhone 8 本身上運行時,我收到錯誤消息,或者它根本不起作用。 我附上了代碼和錯誤。

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
  }
}

[顯示“快照/上傳圖像”及其約束的位置][1]

當應用程序在 iphone 11 max pro 模擬器上運行時,它可以工作,但是在 iphone 8 模擬器上,這是我擁有的設備,它會出現此錯誤

[錯誤][2]

在控制台中:

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) 

[未來讀者的總結:]

結果證明這是一次unrecognized selector崩潰; 由於 OP 沒有提及崩潰或崩潰日志(OP 僅使用“什么都沒有發生”或“無法按下按鈕”之類的短語),診斷變得更加困難。 但我們最終到達了那里。

然而,這是一個稍微不尋常unrecognized selector ,因為 Cocoa 無法識別的 selector 是[Love_In_A_Snap.ViewController takePhoto:] ,我們可以很好地看到 ViewController確實有這個方法:

 @IBAction func takePhoto(_ sender: Any) {

最后,我們不得不通過假設事情已經以某種方式變得“陳舊”來解決這個問題。 我們刪除了 storyboard 中的連接並重新創建它。 那種固定的東西,但不是在所有情況下。 然后我們從設備中刪除了該應用程序並清理了 DerivedData 文件夾,並修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM