我试图通过点击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)
答案 0 :(得分:2)
根据您的描述,您会获得一张图片而不是代码所需的路径,以便您可以这样做:
self.scanDoc.captureImageWithCompletionHander({(image) -> Void in
let captureImageView: UIImageView = UIImageView(image: image)