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 >
You are creating an instance of
FullImageViewController
programmatically but you are using anIBOutlet
in this view controller.You probably want to instantiate it using storyboard, so you'll have to do something like this:
Don't forget to set the identifier for your
FullImageViewController
though.EDIT: Since you're using a segue, you should handle passing the data in
prepareForSegue(_:)
:But accessing
fullimage
before the view is loaded will still cause a crash, so you should try passing the value tofullimage
inviewWillAppear
orviewDidLoad
.In your
FullImageViewController
: