我试图通过点击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,但是在下面的语句中,我们必须传递string。所以我真的很困惑该怎么办。如果有人能找出问题所在。
谢谢您!
UIImage(contentsOfFile: imageFilePath as! String)
最佳答案
根据您的描述,您将得到一个图像,而不是代码期望的路径,因此您可以执行以下操作:
self.scanDoc.captureImageWithCompletionHander({(image) -> Void in
let captureImageView: UIImageView = UIImageView(image: image)