[英]Swift - get UIImage from UIColllectionViewCell
我有一個collectionView
,其中每個cell
都包含一個UIImage
和一個空的UIButton
。 我如何查看用戶點擊的圖片?
class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
@IBOutlet weak var imageCollectionView: UICollectionView!
let images: [UIImage] = [
UIImage(named: "beerImage")!,
UIImage(named: "christmasImage")!,
UIImage(named: "goalImage")!,
UIImage(named: "travelImage")!,
UIImage(named: "rollerImage")!,
UIImage(named: "giftImage")!,
UIImage(named: "shirtImage")!,
UIImage(named: "dressImage")!,
]
let columnLayout = FlowLayout(
itemSize: CGSize(width: 150, height: 150),
minimumInteritemSpacing: 30,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
var colViewWidth: CGFloat = 0.0
override func viewDidLoad() {
self.imageCollectionView.collectionViewLayout = columnLayout
super.viewDidLoad()
imageCollectionView.dataSource = self
imageCollectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
return cell
}
@IBAction func imageButtonTapped(_ sender: Any) {
print("tapped")
}
@IBAction func closeButtonTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! ExampleViewController
self.present(HomeViewController, animated: false, completion: nil)
}
}
更新
最后,我想將該圖像傳遞給另一個 ViewController。 我嘗試過這種方式,但圖片沒有顯示在另一個 ViewController 中:
視圖控制器A
var tappedImage = UIImage()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
tappedImage = images[indexPath.row]
}
var showPopUpView = true
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(showPopUpView)
var vc = segue.destination as! ExampleViewController
vc.pickedImage = self.tappedImage
vc.ShowPopUpView = self.showPopUpView
}
視圖控制器B
var pickedImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
imagePreview.image = pickedImage
你可以做這樣的事情
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
imageToPass = images[indexPath.row]
}
編輯:至於將該圖像傳遞給您的下一個視圖 controller,一種可能的方法是將屬性添加到圖像的第一個和第二個視圖控制器,在您的didSelectItemAt
調用中設置該圖像,然后使用prepare(for segue: UIStoryboardSegue, sender: Any?)
let imageToPass: UIImage?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier name here" {
// Get the new view controller using segue.destination.
if let destVC = segue.destination as? SecondViewController {
// Pass the selected object to the new view controller.
destVC.image = imageToPass
}
}
}
評論中的對話二重奏:
動作的順序應該是:
indexPath
獲取圖像tappedImage
)pickedImage
) 好像你錯過了這些步驟之一(可能是第 3 步,因為你沒有在 ...didSelectItemAt indexPath 中調用performSegue
...didSelectItemAt indexPath...
而且它可能根本沒有被調用!嘗試在那里打印一些東西並檢查它是否是真的)
或者您可能會打亂流程(可能您在目標View
加載后設置圖像,結果是看不到。)
嘗試在這 7 個狀態中打印您可以訪問的所有變量(例如tappedImage
、 pickedImage
、 images[indexPath.row]
等),看看問題出在哪里以及您在哪里丟失了選定的圖像參考
您沒有使用...didSelectItemAt indexPath...
performSegue
執行Segue ,可能您有一個UIButton
,因此該按鈕阻止了cell
被選中。
嘗試這個
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let image = images[indexPath.row]
//Result image
}
(或)如果您想通過按鈕點擊獲取圖像(不推薦)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
cell.imageView.image = images[indexPath.item]
cell.layer.cornerRadius = 2
//Replace your button
cell.yourButton.tag = indexPath.row
cell.yourButton.addTarget(self, action: #selector(self.imageButtonTapped(_:)), for: .touchUpInside)
}
在按鈕動作中
func imageButtonTapped(_ sender: UIButton) {
let image = images[sender.tag]
//Result image
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.