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

标签 swift image phphotolibrary image-quality

我不喜欢苹果图像选择器,所以我决定实现自己的图像选择器。我刚刚完成了获取所有用户照片并在 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/

相关文章:

swift - 将 Alamofire NSData 解析为数组

swift - Firebase DB : If I place observer on posts, 每当单个帖子发生更改时,是否都会返回所有嵌套帖子?

java - 将图片从 URI 复制到文件时图片旋转了 90 度

ios - 如何在照片库中获取重复的媒体?

ios - 使用 PHPhotoLibrary 保存照片时元数据丢失

ios - 锁定方向不适用于 ipad ios 8 swift xcode 6.2

swift - 在 Swift 中转发错误

image - 如何使用 python 语言在 kivy 中定位图像?

javascript - 以特定方式对齐水平列表中的图像

ios - "PHPhotoLibrary.requestAuthorization"在 iOS 9 中不询问权限