2

This is seriously starting to drive me crazy, since it s really simple. I have a collectionviewcell that gets images from phone library

When i tap a cell, i get the index of the fetched collection (i m using Photo Framework), then send the selected image (through PHImageManage's imageFromAsset) to a new view controller, assign this image to a new UIimage property and then try to display it on my UIimageView outlet FAIL ! In the debug Window, I can see that the image is passing from the collectionviewcontroller to my FullImageViewController, and I dont understand why I get an error on my outlet (returns NIL for ever)

here s a bit of code, and a bug capture -- The collectioncontroller first

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: PhotoCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! PhotoCollectionViewCell

 //   cell.contentView.backgroundColor = UIColor.redColor()
    let asset: PHAsset = photosAsset[indexPath.item] as! PHAsset

    PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: cellSize, contentMode: .AspectFit, options: nil) { (reuslt:UIImage?, info:[NSObject : AnyObject]?) -> Void in
        if let image = reuslt {
            cell.displaythunmb(image)
        }
    }

    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let asset=photosAsset[indexPath.item] as! PHAsset
    let size:CGSize = CGSizeMake(200, 200)

    PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: size, contentMode: .AspectFit, options: nil) { (result:UIImage?, info:[NSObject : AnyObject]?) -> Void in
        let controller:FullImageViewController = FullImageViewController()
        if let image = result {
        controller.dislpayFullImage(image)
    }
    }

    performSegueWithIdentifier("gofull", sender: self)

}

and here's the fullImageViewController

class FullImageViewController: UIViewController {
var image:UIImage = UIImage()

@IBOutlet weak var fullimage: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.hidesBarsOnTap = true

}
override func viewWillAppear(animated: Bool) {
    //dislpayFullImage()

}

func dislpayFullImage(fullimg:UIImage){

    if let monimage:UIImage? = fullimg {
        image=monimage!
         fullimage.image = image
    }

}

}

this gives me "fatal error: unexpectedly found nil while unwrapping an Optional value" on the "fullimage.image = image" line

I've tried to unwrap, force unwrap and so on, nothing seems to work

In the debt window, when I follow the properties I have to following :

fullimg UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0 self test.FullImageViewController 0x00007ff3fb6526e0 0x00007ff3fb6526e0 UIKit.UIViewController UIViewController
image UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0

fullimage UIImageView! nil None

monimage UIImage? 0x00007ff3fb6506f0 0x00007ff3fb6506f0

Any idea where I m messing up >

4

1 回答 1

1

您正在以编程方式创建一个实例,FullImageViewController但您IBOutlet在此视图控制器中使用了一个。

您可能想使用情节提要对其进行实例化,因此您必须执行以下操作:

let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let destination = storyboard.instantiateViewControllerWithIdentifier("yourIdentifier") as! FullImageViewController

不要忘记为您设置标识符FullImageViewController

编辑:由于您使用的是segue,您应该处理传递数据prepareForSegue(_:)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "gofull" {
        let destination = segue.destinationViewController as! FullImageViewController
        destination.someImage = result
    }
}

但是fullimage在视图加载之前访问仍然会导致崩溃,所以你应该尝试将值传递给fullimageinviewWillAppearviewDidLoad

在你的FullImageViewController

var someImage: UIImage?

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let image = someImage { fullimage.image = image }
}
于 2016-01-24T04:27:05.627 回答