问题描述
编辑
我当前的代码:
import UIKit
import Photos
import PhotosUI
import Foundation
var selectedImage = UIImage()
extension CGSize {
static func *(lhs: CGSize,rhs: CGFloat) -> CGSize {
.init(width: lhs.width * rhs,height: lhs.height * rhs)
}
}
extension UIImageView {
func fetchImage(asset: PHAsset,contentMode: PHImageContentMode,targetSize: CGSize,version: PHImageRequestOptionsversion = .current,deliveryMode: PHImageRequestOptionsDeliveryMode = .opportunistic) {
let options = PHImageRequestOptions()
options.version = version
options.deliveryMode = deliveryMode
PHImageManager.default().requestimage(for: asset,targetSize: targetSize,contentMode: contentMode,options: options) { image,_ in
guard let image = image else { return }
switch contentMode {
case .aspectFill: self.contentMode = .scaleAspectFill
case .aspectFit: self.contentMode = .scaleAspectFit
@unkNown default: fatalError()
}
self.image = image
}
}
}
class CollectionVC: UICollectionViewController,UICollectionViewDelegateFlowLayout {
var fetchResult: PHFetchResult<PHAsset> = PHFetchResult()
func fetchAssets() {
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
fetchResult = PHAsset.fetchAssets(with: .image,options: fetchOptions)
}
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath as IndexPath)
let asset = fetchResult.object(at: indexPath.row)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.fetchImage(asset: asset,contentMode: .aspectFill,targetSize: imageView.frame.size * UIScreen.main.scale )
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
fetchAssets()
}
override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return fetchResult.count
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 6
return CGSize(width: width,height: width)
}
func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
return 6.0
}
func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 6.0
}
}
这是我所有的集合视图控制器代码。这是我得到的结果。 苹果图像选择器: 我的图片选择器: 我知道我的操作是自上而下的,但是对于图像,您可以看到它的放大程度更大。在情节提要中,我有一个图像视图,其标签与该单元格水平和垂直对齐。除此之外,默认情况下我还具有所有图像属性。然后我有一个单元格,其标识符为Cell和默认属性。这是我所有的关于Collection视图控制器的代码和情节提要,但我仍然得到这些结果。有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)