[英]UIImageView passing (segue) nil to another ViewController
after a day of researching and trying stuff, I come to your aid. 经过一天的研究和尝试,我帮助了您。
I have a collectionView passing an image to an imageView, just like instagram (for you to imagine the interface), I THINK I'm performing the segue right, but on the other viewController it ends up NIL. 我有一个collectionView将图像传递到imageView,就像instagram(让您想象接口)一样,我认为我正在正确执行segue,但是在另一个viewController上,它最终为NIL。
My code is as follows: 我的代码如下:
First View Controller > 第一视图控制器>
// TakePhotoViewController.swift
import UIKit
import Photos
class TakePhotoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var photoImageView: UIImageView!
var imageArray = [UIImage]()
override func viewDidLoad(){
super.viewDidLoad()
grabPhotos()
}
@IBAction func postPhotoTaken(_ sender: Any) {
self.performSegue(withIdentifier: "photoPost", sender: self)
}
func grabPhotos(){
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
if let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
if fetchResult.count > 0 {
for i in 0..<fetchResult.count{
imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {image, error in
self.imageArray.append(image!)
})
}
}
else {
print("You Don't Have Any Photos!")
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
photoImageView.image = imageArray[indexPath.row]
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "photoPost" {
let photoPost = segue.destination as! PhotoPostTableViewController
let imagePhoto = self.photoImageView.image
photoPost.photo = imagePhoto!
}
}
}
Second View Controller > 第二视图控制器>
// photoPostViewController.swift
import UIKit
class PhotoPostTableViewController: UITableViewController {
var photo: UIImage!
@IBOutlet weak var newPhoto: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
newPhoto.image = photo
print(photo)
}
override func viewDidAppear(_ animated: Bool) {
newPhoto.image = photo
print(photo)
}
}
Can you guys help me? 你们能帮我吗?
Based on the fact that the segue.identifier
in your prepare(for:)
returns nil, that means the segue performed is triggered by the storyboards, not by the postPhotoTaken(_ sender: Any)
. 基于您
prepare(for:)
中的segue.identifier
返回nil的事实,这意味着执行的segue是由情节postPhotoTaken(_ sender: Any)
触发的,而不是由postPhotoTaken(_ sender: Any)
触发的。
Check the storyboard, and find the segue that goes from first VC to second VC and is triggered by the button, and change it's identifier
to "photoPost"
. 检查情节提要,找到从第一个VC到第二个VC并由按钮触发的segue,然后将其
identifier
更改为"photoPost"
。
I believe after that you can delete postPhotoTaken(_ sender: Any)
. 我相信之后,您可以删除
postPhotoTaken(_ sender: Any)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.