gpt4 book ai didi

swift - 为什么图像质量这么低? ( swift )

转载 作者:行者123 更新时间:2023-12-05 03:49:59 32 4
gpt4 key购买 nike

我不喜欢苹果图像选择器,所以我决定实现自己的图像选择器。我刚刚完成了获取所有用户照片并在 Collection View 中显示它们的阶段,尽管我注意到图像质量的差异非常可怕。这是我的代码:

import UIKit
import Photos
import PhotosUI
import Foundation

private let reuseIdentifier = "Cell"
var selectedImage = UIImage()

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {


var imageArray = [UIImage]()

override func viewDidLoad() {
super.viewDidLoad()

grapPhotos()



}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return imageArray.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
let imageView = cell.viewWithTag(1) as! UIImageView


cell.layer.cornerRadius = 4
imageView.image = imageArray[indexPath.row]


return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let selectedImageName = imageArray[indexPath.item]
print(selectedImageName)
selectedImage = selectedImageName
performSegue(withIdentifier: "Custom", sender: self)
}

func grapPhotos() {

let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat


let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(format: "mediaType = %d || mediaType = %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: fetchOptions) {

if fetchResult.count > 0 {

for i in 0..<fetchResult.count {
imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

image, error in


self.imageArray.append(image!)


})


}
}
else {
self.collectionView?.reloadData()
print("No Photos")
}
}
}
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, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

return 6.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

return 6.0
}
}

我不太了解如何处理图像,所以如果有人能帮助我显示更高质量的图像,那就太好了。

最佳答案

这真的有效))

for index in 0..<fetchResult.count {
let asset = fetchResult.object(at: index) as PHAsset
let sizeFactor = UIScreen.main.scale
let deviceSize = UIScreen.main.nativeBounds.size
manager.requestImage(for: asset,
targetSize: CGSize(width: deviceSize.width * sizeFactor,
height: deviceSize.height * sizeFactor),
contentMode: .aspectFit,
options: requestOptions,
resultHandler: { (uiimage, info) in
if let image = uiimage {
allImages.append(image)
}
})
}

你只需要知道->

let sizeFactor = UIScreen.main.scale
let deviceSize = UIScreen.main.nativeBounds.size

关于swift - 为什么图像质量这么低? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63691922/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com