我正在开发一款带照片部分和收藏视图的应用。单元格中的图像模糊不清。在全屏查看时,它们很清晰,在桌面视图中以缩略图的形式显示在不同的屏幕上。我有一个名为Photo的NSManagedObject子类导入Photos,引用PHAssets,并包含名为getAsset()和getImage(size)的方法。
以下是我的UICollectionViewCell子类的代码:
class PhotoCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageForCell: UIImageView!
@IBOutlet weak var labelForCell: UILabel!
func setImage (photo: Photo?, size: CGSize) -> Bool {
if photo != nil {
if let image = photo?.getImage(size) {
var label = photo?.getLabel()
imageForCell.contentMode = UIViewContentMode.ScaleAspectFit
imageForCell.image = image
labelForCell.text = label
return true
}
}
return false
}
来自我的UICollectionView子类中的cellForItemAtIndex:
if let photo: Photo = listOfPhotos[indexPath.item] as? Photo {
var size: CGSize = CGSizeMake(500, 350)
if !cell.setImage(photo, size: size) {
// Handles a missing photo.
}
}
来自Photo NSManagedObject子类的2个方法根据创建日期查找照片:
func getAsset() -> PHAsset? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "creationDate = %@", photoDate)
let assets = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: fetchOptions)
var asset: PHAsset?
if assets.count == 1 {
asset = assets.lastObject as? PHAsset
}
else {
// Handles invalid result
}
return asset
}
func getImage(size: CGSize) -> UIImage? {
var image: UIImage?
if let asset = getAsset() {
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: size, contentMode: PHImageContentMode.AspectFit , options: nil) { (result, info) -> Void in
image = result
println("P gI: Image gotten. Image Size: \(image?.size), Size: \(size), Date: \(asset.creationDate)")
}
}
else {
}
return image
}
getImage中的println()语句向我展示了这是以不同的大小获取两次图像,两者都不是指定的大小。首先获取每个单元的较小尺寸,然后是每个单元的较大尺寸。我怀疑较小的图像被炸毁以填满我的细胞(我认为它会使它变得模糊)并且它不会加载到更大的尺寸(我认为很清楚)。记录输出:
P gI: Image gotten. Image Size: Optional((64.0,48.0)), Size: (500.0,350.0), Date: 2014-12-04 18:56:20 +0000
P gI: Image gotten. Image Size: Optional((64.0,48.0)), Size: (500.0,350.0), Date: 2014-12-04 18:56:36 +0000
P gI: Image gotten. Image Size: Optional((2048.0,1530.0)), Size: (500.0,350.0), Date: 2014-12-04 18:56:20 +0000
P gI: Image gotten. Image Size: Optional((2048.0,1530.0)), Size: (500.0,350.0), Date: 2014-12-04 18:56:36 +0000
在我的故事板中,我的imageView和单元格都设置为250 x 175.我的imageView在检查器中设置为Aspect Fit。
答案 0 :(得分:1)
Photos框架首先获取较小的尺寸以获得速度,然后获取较大的图像尺寸 您可以通过设置请求选项告诉它仅获取较大的图像:
let requestOptions = PHImageRequestOptions()
requestOptions.deliveryMode = .HighQualityFormat
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: size, contentMode: PHImageContentMode.AspectFit , options: requestOptions) { (result, info) in
// ...
}
请参阅PHImageRequestOptionsDeliveryMode
了解更多选项。