【问题标题】:Why is the image quality so low? (swift)为什么画质这么差? (迅速)
【发布时间】:2020-12-20 19:40:10
【问题描述】:

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

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

我对处理图像知之甚少,所以如果有人可以帮助我展示更高质量的图像,那就太好了。

【问题讨论】:

  • 你为什么使用 .aspectFill 而不是 .aspectFit ?前者(aspectFill)会扭曲图像,如果它的大小与容器成比例。
  • @claude31 因为我在集合视图中有图像,所以我希望所有图像的大小都相同
  • 是的,但是 aspectFill 可能会扭曲它们。是你遇到的问题吗?
  • 但我查看了其他应用程序,它们具有高质量的纵横比填充图像。我认为这与@rohanphadte 发布的答案有关
  • 它是否适用于比例因子?通常这会使图像变大但不会改变质量。感谢举报。

标签: swift image phphotolibrary image-quality


【解决方案1】:

图像质量取决于您观看时使用的手机 - 视网膜屏幕需要更多像素。您需要将 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,一个点可以分别用九个或四个像素表示。对于标准分辨率显示器,比例因子为 1.0,1 点等于 1 个像素。

【讨论】:

  • 我对你说的有点困惑。我假设您的意思是两件事之一:首先,我必须更改目标大小,在这种情况下我不知道该怎么做。其次,它只取决于我的手机,但这让我想到为什么我手机上的其他图像质量更高?
  • 更改目标大小意味着更改requestImage函数中的targetSize: CGSize(width: 200 * UIScreen.main.scale, height: 200.0 * UIScreen.main.scale)。我不确定您所说的“为什么我手机上的其他图像质量更高”是什么意思。您是在苹果的图像选择器中引用手机上的其他图像,还是使用您制作的这个集合视图?
  • 快速更新我最终将代码更改为:targetSize: CGSize(width: 150, height: 150 * UIScreen.main.scale * UIScreen.main.scale * UIScreen.main.scale 虽然这会放大图像,但我似乎获得了最高质量,所以我想知道如何不这样做。
  • 为什么要乘以3倍的高度?
【解决方案2】:

这确实有效))

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

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2020-04-16
    • 2016-11-28
    • 1970-01-01
    • 2023-03-06
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多