我有一个collectionView将图像传递给imageView,就像instagram一样(你可以想象界面),我认为我正在执行segue,但在另一个viewController上它最终没有。
我的代码如下:
第一视图控制器>
// 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!
}
}
}
第二视图控制器&gt;
// 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)
}
}
你们能帮助我吗?
答案 0 :(得分:1)
基于segue.identifier
中prepare(for:)
返回nil的事实,这意味着执行的segue是由故事板触发的,而不是postPhotoTaken(_ sender: Any)
触发的。
检查故事板,找到从第一个VC到第二个VC并且由按钮触发的segue,并将其identifier
更改为"photoPost"
。
我相信您可以删除postPhotoTaken(_ sender: Any)
。