从集合视图传递到视图控制器的简单图像数据失败

时间:2016-01-24 04:14:08

标签: ios swift uiimage uicollectionview optional-values

这严重开始让我发疯,因为它非常简单。 我有一个从电话库中获取图像的collectionviewcell

当我点击一个单元格时,我获取所获取集合的索引(即时通讯使用Photo Framework),然后将所选图像(通过PHImageManage的imageFromAsset)发送到新的视图控制器,将此图像分配给新的UIimage属性,然后尝试在我的UIimageView插座上显示它 失败! 在调试窗口中,我可以看到图像从collectionviewcontroller传递到我的FullImageViewController,我不明白为什么我的插座出现错误(永远返回NIL)

这里有一些代码和一个bug捕获 - 第一个是collectioncontroller

 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)

}

这里是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
    }

}

}

这给了我“致命错误:在”fullimage.image = image“行中展开”可选值“时意外发现nil

我试图打开包装,强行打开包装等等,似乎没什么用处

在债务窗口中,当我按照我必须遵循的属性时:

fullimg UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0 self test.FullImageViewController 0x00007ff3fb6526e0 0x00007ff3fb6526e0 UIKit.UIViewController UIViewController
图像UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0

fullimage UIImageView!无无

monimage UIImage? 0x00007ff3fb6506f0 0x00007ff3fb6506f0

任何想法搞砸的地方>

1 个答案:

答案 0 :(得分:1)

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

您可能希望使用storyboard实例化它,因此您必须执行以下操作:

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仍然会导致崩溃,因此您应该尝试将值传递到fullimageviewWillAppear中的viewDidLoad

FullImageViewController

var someImage: UIImage?

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

    if let image = someImage { fullimage.image = image }
}