【问题标题】:Could not cast value of type 'UIImage'to 'NSString'无法将“UIImage”类型的值转换为“NSString”
【发布时间】:2016-07-19 06:39:22
【问题描述】:

我正在尝试通过点击 captureDoc 按钮来捕获图像。但是当我点击捕获按钮时,我收到一个错误“无法将 UIImage 类型的值转换为 NSString”。下面是我的操作按钮代码。

@IBAction func captureDoc(sender: AnyObject) {
    weak var weakSelf = self

    self.scanDoc.captureImageWithCompletionHander({(imageFilePath) -> Void in
        let captureImageView: UIImageView = UIImageView(image: UIImage(contentsOfFile: imageFilePath as! String))
        captureImageView.backgroundColor = UIColor(white: 0.0, alpha: 0.7)
        captureImageView.frame = CGRectOffset(weakSelf!.view.bounds, 0, -weakSelf!.view.bounds.size.height)
        captureImageView.alpha = 1.0
        captureImageView.contentMode = .ScaleAspectFit
        captureImageView.userInteractionEnabled = true
        weakSelf?.view.addSubview(captureImageView)
        let dismissTap: UITapGestureRecognizer = UITapGestureRecognizer(target: weakSelf, action: #selector(self.dismissPreview))
        captureImageView.addGestureRecognizer(dismissTap)
        UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.7, options: .AllowUserInteraction, animations: {
            captureImageView.frame = weakSelf!.view.bounds

            }, completion: nil)
    })
}

我收到此错误是因为我将 imageFilePath 作为 AnyObject,但在下面的语句中我们必须传递字符串。所以我真的很困惑该怎么办。如果有人能弄清楚问题是什么。 谢谢!

UIImage(contentsOfFile: imageFilePath as! String)

【问题讨论】:

  • 好像你已经得到了一个图像,而不是图像的路径
  • 是的,现在我该怎么办?? @Wain

标签: swift string uiimage


【解决方案1】:

根据您的描述,您会得到一个图像,而不是您的代码期望的路径,因此您可以这样做:

self.scanDoc.captureImageWithCompletionHander({(image) -> Void in
    let captureImageView: UIImageView = UIImageView(image: image)

【讨论】:

  • 现在我收到“无法转换 'AnyObject!”类型的值的错误消息到预期的参数类型“UIImage?”。我的图像是 AnyObject。 @wain
猜你喜欢
  • 2019-02-03
  • 2019-01-10
  • 2016-06-06
  • 2021-10-02
  • 2017-09-26
  • 1970-01-01
  • 2017-11-22
  • 2021-03-08
  • 2017-04-09
相关资源
最近更新 更多