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

为什么图像质量这么低?(swift),swift,image,phphotolibrary,image-quality,Swift,Image,Phphotolibrary,Image Quality,我不喜欢苹果图像采集器,所以我决定实现我自己的。我刚刚完成了获取所有用户照片并将其显示在集合视图中的阶段,尽管我注意到图像质量的差异非常可怕。这是我的密码: import UIKit import Photos import PhotosUI import Foundation private let reuseIdentifier = "Cell" var selectedImage = UIImage() class CollectionVC: UICollectio

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

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
    }
}
导入UIKit
导入照片
导入PhotosUI
进口基金会
private let reuseIdentifier=“Cell”
var selectedImage=UIImage()
类CollectionVC:UICollectionViewController,UICollectionViewDelegateFlowLayout{
var imageArray=[UIImage]()
重写func viewDidLoad(){
super.viewDidLoad()
照片()
}
重写func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回imageArray.count
}
重写func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“cell”,for:indepath作为indepath)
让imageView=cell.viewWithTag(1)为!UIImageView
cell.layer.cornerRadius=4
imageView.image=imageArray[indexPath.row]
返回单元
}
重写func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
让selectedImageName=imageArray[indexPath.item]
打印(选择图像名称)
selectedImage=selectedImageName
性能检查(标识符为“自定义”,发送者为自我)
}
函数{
让imgManager=PHImageManager.default()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode=.highQualityFormat
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:false)]
fetchOptions.predicate=NSPredicate(格式:“mediaType=%d | | mediaType=%d”,PHAssetMediaType.image.rawValue,PHAssetMediaType.video.rawValue)
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:fetchOptions){
如果fetchResult.count>0{
对于0..CGSize中的i{
let width=collectionView.frame.width/3-6
返回CGSize(宽度:宽度,高度:宽度)
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumLineSpacingforSection:Int)->CGFloat{
返回6.0
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumItemSpacingForSection:Int)->CGFloat{
返回6.0
}
}

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

图像质量取决于手机,在视网膜上观看屏幕需要更多像素。你需要将targetSize乘以
UIScreen.main.scale

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


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

图像质量取决于手机,您在视网膜上观看屏幕需要更多像素。您需要将targetSize乘以
UIScreen、 主刻度盘

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


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

为什么使用.aspectFill而不是.aspectFit?前者(aspectFill)如果图像的大小与容器的大小成比例,则会扭曲图像。@claude31因为我在集合视图中有图像,我希望所有图像的大小都相同,但aspectFill可能会扭曲图像。这是您遇到的问题吗?但我看了其他应用程序,它们有高质量的aspect fill图像。我认为这与e answer@rohanphadte posted它与比例因子一起工作吗?通常这会使图像变大,但不会改变质量。这要感谢report。为什么使用.aspectFill而不是.aspectFit?前者(aspectFill)如果图像的大小与容器的大小成比例,则会扭曲图像。@claude31因为我在集合视图中有图像,我希望所有图像的大小都相同,但aspectFill可能会扭曲图像。这是您遇到的问题吗?但我看了其他应用程序,它们有高质量的aspect fill图像。我认为这与e answer@rohanphadte Posted它是否与比例因子一起工作?通常这会使图像变大,但不会改变质量。多亏了报告。我对你所说的有点困惑。我想你的意思是两件事之一:首先,我必须改变目标大小,在这种情况下我不知道如何做。其次,这取决于但这就引出了一个问题:为什么我手机上的其他图像质量更高?更改目标大小意味着更改
targetSize:CGSize(宽度:200*UIScreen.main.scale,高度:200.0*UIScreen.main.scale)
函数中的
请求图像
功能。我不确定你的意思是什么“为什么我手机上的其他图像具有更高的质量”。您是指苹果的图像采集器中手机上的其他图像,还是使用该收藏视图