来自UIView图像捕获的UIImage不断崩溃? Swift iOS9

时间:2016-09-11 21:45:57

标签: ios swift uiview uiimage

这是一个奇怪的问题,因为有时代码工作正常,有时候代码崩溃。但最近它只是崩溃了。

我有一张我从uiview获得的图片。我使用下面的捕获函数抓取图像然后将图像发送到属性以转到prepareForSegue发送到下一个视图控制器。当代码返回图像时,代码会一直崩溃。我对它进行了调试,看到该属性肯定是从nil到包含图像,但是当它有时间让它崩溃时。但崩溃是在捕获函数内而不是segue。这很奇怪,因为图像被返回(调试器显示它),但捕获返回是崩溃不断发生的地方。崩溃发生在:

//This seems to be the main problem
return capturedImage

并且这里是返回的captureImage的崩溃日志

Thread1: EXC_BAD_INSTRUCTION (code=EXC_1336_INVOP, subcode=0x0)

我在这条线路上也遇到了崩溃但不知何故崩溃停止了。我不能为这个崩溃日志,因为它停止了崩溃,但我不知道为什么或如何停止

view!.layer.renderInContext(UIGraphicsGetCurrentContext()!)

有什么想法吗?

顺便说一下,我使用故事板按钮来触发segue。

class MainController: UIViewController {

@IBOutlet weak var mainView: UIView!

var mainPic: UIImage? 


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

//MARK: -Func to convert image in uiview to uiimage
//get image from UIView, render it for Retina compatibility, then return it as an UIImage
func captureAndRenderUIImageFromUIView(view:UIView?) -> UIImage {

    UIGraphicsBeginImageContextWithOptions((view?.bounds.size)!, true, 0.0)
    view!.drawViewHierarchyInRect(view!.bounds, afterScreenUpdates: true)

    //I was also getting crashes on this line but they stopped??
    view!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let capuredImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //The return value is where the crash keeps happening
    return capturedImage
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

     self.mainPic = self.captureAndRenderUIImageFromUIView(mainView)

     if segue.identifier == "toDetailController"{

     let detailVC = segue.destinationViewController as! DetailController
     detailVC.detailPic = self.mainPic!
    }
 }

}

}

以下是

的观点
class DetailController: UIViewController {
@IBOutlet weak var imageView: UIImageView!

var detailPic: UIImage? 

override func viewDidLoad() {
     super.viewDidLoad()
     self.imageView.image = self.detailPic
   }

}

1 个答案:

答案 0 :(得分:0)

您好:我开始尝试的方式与您尝试过的方式非常相似;但是,它容易出错并且不稳定。我最近发现通过使用UIView方法snapshotViewAfterScreenUpdates可以节省很多麻烦。只需在要拍摄快照的视图上直接调用它,它就会返回一个UIImage。

您可以在以下网址查看更多详情: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/snapshotViewAfterScreenUpdates

事实上我也在这里学到了一些东西,因为似乎有一系列这些函数(例如,一个函数可以为你提供一个可调整大小的UIImageView)。

希望有所帮助。

编辑:在我的一个应用程序中,我通过子程序实现了如下:

func getSnapshot(target: UIView) -> Void {
            // Clean down
            if let _ = self.snapshotView where self.snapshotView.isDescendantOfView(self) {
                self.snapshotView.removeFromSuperview()
            }

            self.snapshotView = target.snapshotViewAfterScreenUpdates(true)
            let snapshotFrame : CGRect = CGRect(origin: recognizer.locationInView(self), size: target.frame.size)
            self.snapshotView.frame = snapshotFrame
            self.addSubview(self.snapshotView)

            self.canMoveSnapshot = true
        }