我有一个UICollectionView,其中包含包含UIImages的单元格。这是我的didSelectIndex方法来抓取图像并将其全屏显示。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let media = self.media[indexPath.row]
guard let photoURL = URL(string: media.photoURL) else { return }
// Get the image from the cache
if let image = mediaCache.object(forKey: photoURL.absoluteString as AnyObject) as? UIImage {
let imageView = UIImageView(image: image)
imageView.frame = UIScreen.main.bounds
imageView.backgroundColor = .black
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.isUserInteractionEnabled = true
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
let dismissTap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
imageView.addGestureRecognizer(dismissTap)
self.view.addSubview(imageView)
}
}
但是,将内容模式从scaleAspectFill更改为scaleAspectFit并不能解决问题。图像不适合UIImageView框架。