This is seriously starting to drive me crazy, since it s really simple. I have a collectionviewcell that gets images from phone library
这真的让我开始疯狂,因为它真的很简单。我有一个collectionviewcell,它从电话库获取图像
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)
当我点击一个细胞,我得到获取集合的索引(我使用照片框架),然后把选中的图像(通过PHImageManage imageFromAsset)到一个新的视图控制器,将这个图像分配给新的用户界面图像属性,然后试图显示它在我UIimageView出口失败!在debug窗口中,我可以看到图像正在从collectionviewcontroller传递到我的FullImageViewController,我不明白为什么我的outlet中会出现错误(永远返回NIL)
here s a bit of code, and a bug capture -- The collectioncontroller first
这里有一些代码,还有一个bug捕获——首先是集合控制器
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
这是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
这给了我“致命错误:意外地发现了nil,同时打开了一个可选的值”在“fullimage”上。形象=形象”
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
fullimg UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0自我测试。FullImageViewController 0 x00007ff3fb6526e0 0 x00007ff3fb6526e0 UIKit。UIViewController图像UIImage 0x00007ff3fb6506f0 0x00007ff3fb6506f0
fullimage UIImageView! nil None
fullimage UIImageView !零没有
monimage UIImage? 0x00007ff3fb6506f0 0x00007ff3fb6506f0
monimage界面图像吗?0 x00007ff3fb6506f0 0 x00007ff3fb6506f0
Any idea where I m messing up >
我搞砸了>
1 个解决方案
#1
1
You are creating an instance of FullImageViewController
programmatically but you are using an IBOutlet
in this view controller.
您正在以编程方式创建FullImageViewController的一个实例,但是您正在这个视图控制器中使用IBOutlet。
You probably want to instantiate it using storyboard, so you'll have to do something like this:
你可能想要用故事板来实例化它,所以你必须这样做:
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let destination = storyboard.instantiateViewControllerWithIdentifier("yourIdentifier") as! FullImageViewController
Don't forget to set the identifier for your FullImageViewController
though.
但是不要忘记为FullImageViewController设置标识符。
EDIT: Since you're using a segue, you should handle passing the data in prepareForSegue(_:)
:
编辑:既然你在使用segue,你应该在prepareForSegue中处理传递数据(_:):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "gofull" {
let destination = segue.destinationViewController as! FullImageViewController
destination.someImage = result
}
}
But accessing fullimage
before the view is loaded will still cause a crash, so you should try passing the value to fullimage
in viewWillAppear
or viewDidLoad
.
但是在加载视图之前访问fullimage仍然会导致崩溃,所以您应该尝试在viewWillAppear或viewDidLoad中将值传递给fullimage。
In your FullImageViewController
:
在你FullImageViewController:
var someImage: UIImage?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let image = someImage { fullimage.image = image }
}
#1
1
You are creating an instance of FullImageViewController
programmatically but you are using an IBOutlet
in this view controller.
您正在以编程方式创建FullImageViewController的一个实例,但是您正在这个视图控制器中使用IBOutlet。
You probably want to instantiate it using storyboard, so you'll have to do something like this:
你可能想要用故事板来实例化它,所以你必须这样做:
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let destination = storyboard.instantiateViewControllerWithIdentifier("yourIdentifier") as! FullImageViewController
Don't forget to set the identifier for your FullImageViewController
though.
但是不要忘记为FullImageViewController设置标识符。
EDIT: Since you're using a segue, you should handle passing the data in prepareForSegue(_:)
:
编辑:既然你在使用segue,你应该在prepareForSegue中处理传递数据(_:):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "gofull" {
let destination = segue.destinationViewController as! FullImageViewController
destination.someImage = result
}
}
But accessing fullimage
before the view is loaded will still cause a crash, so you should try passing the value to fullimage
in viewWillAppear
or viewDidLoad
.
但是在加载视图之前访问fullimage仍然会导致崩溃,所以您应该尝试在viewWillAppear或viewDidLoad中将值传递给fullimage。
In your FullImageViewController
:
在你FullImageViewController:
var someImage: UIImage?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let image = someImage { fullimage.image = image }
}