Passing a CKAsset image through a segue to ViewController?

65 Views Asked by At

I have a simple TableView with text and an image being pulled from CloudKit. I'm trying to pass that data on to a detailViewController when selected, I can get the text, but I'm stuck on sending the image though prepareForSegue.

I have a TableViewController and DetailViewController.

TableViewController has cells with image and text from CK.

DetailViewController has UIImageView and Labels to be populated.

Here is the code..

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetailView" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let destinationController = segue.destinationViewController as! DetailViewController

            let result = results[indexPath.row]
            let image = result.objectForKey("Image1") as! CKAssset
            destinationController.imageViewMain.image = UIImage(data: NSData(contentsOfURL: image.fileURL)!)

        }
    }
}

It's not just not working, I'm actually getting a crash with EXC_BAD_INSTRUCTION when I select a table row. The tableView works fine and gets the data/image from the database, just can't seem to get it passed through the segue.

Any help or pointing in the right direction would be appreciated.

Thanks

1

There are 1 best solutions below

0
On

After some work I managed to figure this out.

For anyone else that might be trying the same thing...

Basically I was trying to create the UIImage inside prepareForSegue function and while that may be possible, I couldn't get it working.

My solution was to just pass the CKAsset through the segue and then in DetailViewController I was able to use the CKAsset to create a UIImage by passing the fileURL from the CKAsset.

I'm still learning, so the way I did it maybe totally wrong.