I got Apple's sample code for 'SamplePhotosApp' and in the photo album grid photo layout, trying to detect DNG RAW files (put up a badge if it's DNG).
Default cellForItemAt:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let asset = fetchResult.object(at: indexPath.item)
// Dequeue a GridViewCell.
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: GridViewCell.self), for: indexPath) as? GridViewCell
else { fatalError("unexpected cell in collection view") }
// Add a badge to the cell if the PHAsset represents a Live Photo.
if asset.mediaSubtypes.contains(.photoLive) {
cell.livePhotoBadgeImage = PHLivePhotoView.livePhotoBadgeImage(options: .overContent)
}
// Request an image for the asset from the PHCachingImageManager.
cell.representedAssetIdentifier = asset.localIdentifier
imageManager.requestImage(for: asset, targetSize: thumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: { image, _ in
// The cell may have been recycled by the time this handler gets called;
// set the cell's thumbnail image only if it's still showing the same asset.
if cell.representedAssetIdentifier == asset.localIdentifier {
cell.thumbnailImage = image
}
})
return cell
}
DNG/RAW Format
With DNG files, there could be a preview or thumbnail (with iOS11) embedded in it, and of course a completely separate JPEG attached to it.
With above code, requestImage still displays DNG files by pulling out its embedded JPEG. However, it doesn't know that the PHAsset is actually a DNG file.
How can I find out if the PHAsset is a DNG?
Things I Tried
let fileExtension = ((asset.value(forKey: "uniformTypeIdentifier") as! NSString).pathExtension as NSString).uppercased
if fileExtension == "DNG" || fileExtension == "RAW-IMAGE" {
//Show RAW Badge
}
Above works only if the DNG file has just the preview JPEG embedded. If it has a regular full-size JPEG embedded, it recognize the PHAsset as a JPEG.
I've been told to try this:
let res = PHAssetResource.assetResources(for: asset)
But a certain asset could have several number of resources (adjustment data, etc). How would I be able to make this work?