如何解决如何在不放大的情况下使图像具有良好的质量快速
编辑
我当前的代码:
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 举报,一经查实,本站将立刻删除。