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

Why is the image quality so low? (swift)

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

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
    }
}

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

图像质量取决于 phone 您的观看 - 视网膜屏幕需要更多像素。您需要将 targetSize 乘以 UIScreen.main.scale.

尝试在 imgManager.requestImage 函数中设置 targetSize: CGSize(width: 200 * UIScreen.main.scale, height: 200.0 * UIScreen.main.scale)

For Retina displays,比例因子可以是3.0或2.0,一个点可以分别用9个或4个像素表示。对于 standard-resolution 显示器,比例因子为 1.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