This is an odd issue because sometimes the code works fine and others times it crashes. But lately it's only been crashing.
I have an image that I'm getting from a uiview. I grab the image using the capture function below then send the image to a property to go to prepareForSegue to be sent to the next view controller. The code keeps crashing when it's time to return the image. I debugged it and saw the property definitely went from nil to containing the image however when it's time to segue it crashes. But the crash is within the capture function not the segue. It's weird because the image is returned (debugger showed it) but the capture return is where the crash keeps happening. The crash happens on:
//This seems to be the main problem
return capturedImage
and here's the crash log for the returned capturedImage
Thread1: EXC_BAD_INSTRUCTION (code=EXC_1336_INVOP, subcode=0x0)
I was also getting crashes on this line but somehow the crashes stopped. I can't put a crash log for this one because it stopped crashing but I don't know why or how it stopped
view!.layer.renderInContext(UIGraphicsGetCurrentContext()!)
Any ideas?
Btw I use a storyboard button to trigger the 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!
}
}
}
}
Here's the view it's going to
class DetailController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var detailPic: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = self.detailPic
}
}