微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么图像质量这么低? 迅速

如何解决为什么图像质量这么低? 迅速

我不喜欢苹果图像选择器,所以我决定实现自己的图像选择器。我刚刚完成了获取所有用户照片并将其显示在收藏夹视图中的阶段,尽管我注意到图像质量的差异令人震惊。这是我的代码

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,minimumLinespacingForSectionAt section: Int) -> CGFloat {
        
        return 6.0
    }
    func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        
        return 6.0
    }
}

我对使用图像真的不了解,所以如果有人可以帮助我显示更高质量的图像,那会很棒。

解决方法

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

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

For Retina displays,比例因子可以是3.0或2.0,一个点可以分别由九个或四个像素表示。对于标准分辨率的显示器,比例因子为1.0,一个点等于一个像素。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。