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
}
}
我对使用图像知之甚少,所以如果有人可以帮助我展示更高质量的图像,那就太好了。
解决方案
图像质量取决于您观看时使用的手机 - 视网膜屏幕需要更多像素。您需要将 targetSize 乘以UIScreen.main.scale
.
尝试targetSize: CGSize(width: 200 * UIScreen.main.scale, height: 200.0 * UIScreen.main.scale)
在您的imgManager.requestImage
功能中设置。
对于 Retina 显示器,比例因子可以是 3.0 或 2.0,一个点可以分别由九个或四个像素表示。对于标准分辨率显示器,比例因子为 1.0,1 点等于 1 个像素。
推荐阅读
- perl - 在perl中使用命名管道在2个进程之间进行通信?
- javascript - 将受污染的画布下载为 PNG
- python - 正则表达式也不返回分隔模式
- python - Tkinter Image 仅显示最后一张
- php - 如何使用 PHP 从 JSON 中的数组中获取值?
- c# - ActionBlock B 从不接收 TransformBlock A 返回的项目
- java - 如何在循环中检查Java中对象的所有部分
- html - 页脚底部内容不会粘在底部
- python-3.x - 如何使用 psycopg2 将数据插入 postgres 数据库
- android - 获取列表
> 平面图操作后