繁体   English   中英

从UIView图像捕获的UIImage不断崩溃? 迅捷的iOS9

[英]UIImage from UIView image capture keeps crashing? Swift iOS9

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

我有一张从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
   }

}

嗨,我开始尝试做事情的方式与您在这里尝试过的方式差不多; 但是,它容易出错且不稳定。 我最近发现,使用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
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM