[英]How to display image of selected UICollectioNViewCell in UIView? (Swift 3)
I am new to iOS development, so please bear with me if this is obvious. 我是iOS开发的新手,因此,如果这很明显,请耐心等待。
I have a UICollectionView on the bottom half of a view controller; 我在视图控制器的下半部分有一个UICollectionView; it currently displays all photos in my photo library.
它当前显示我照片库中的所有照片。 There is a UIView on the top half of the same view controller.
同一视图控制器的上半部分有一个UIView。
Q: When I select a UICollectionViewCell (ie a photo), how do I display that photo in the UIView on the same view controller? 问:当我选择UICollectionViewCell(即照片)时,如何在同一视图控制器上的UIView中显示该照片?
I imagine I'll have to use didSelectItemAt somehow to save the index path of the selected image in order to display that image in the UIView. 我想我将不得不使用didSelectItemAt以某种方式保存所选图像的索引路径,以便在UIView中显示该图像。 (I've included what I have thus far for didSelectItemAt.) How do I refresh the content of the UIView as different photos are selected?
(我已经包括了迄今为止对didSelectItemAt的支持。)当选择了不同的照片时,如何刷新UIView的内容?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedVideo = collectionView.cellForItem(at: indexPath)
selectedVideo?.alpha = 0.5
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deselectedVideo = collectionView.cellForItem(at: indexPath)
deselectedVideo?.alpha = 1.0
}
put ImageView
into your view then make IBOutlet
of imageview . 将
ImageView
放入您的视图中,然后制作imageview的IBOutlet
。 you get cell of collectionview , set image of collectionviewcell
of imageview
image into top imageView
您获得collectionview的单元格,将
imageview
图像的collectionviewcell
的图像设置为顶部imageView
You can try this 你可以试试这个
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = collectionView.cellForItem(at: indexPath) as! MyCollectionViewCell
yourview.imageView.image = selectedCell.imageView.image
}
From my understanding on the comments you have described earlier, there are 2 ways you can implement. 根据我对您前面描述的注释的理解,可以使用两种方法。
I'm assuming that you are using the 1st method, 我假设您使用的是第一种方法,
Drop an @IBOutlet
for the above UIImageView and name it myImageView
for example. 为上面的UIImageView放置一个
@IBOutlet
,并将其myImageView
为myImageView
。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedImage = collectionView.cellForItem(at: indexPath)
myImageView.image = selectedImage.myImageView.image
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedVideo = collectionView.cellForItem(at: indexPath)
view.imageView.image = selectedVideo
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.