[英]iOS: PHAsset & Detecting DNG RAW Format
我得到了Apple的“ SamplePhotosApp ”示例代碼,並且在相冊網格照片布局中,試圖檢測DNG RAW文件(如果是DNG,則輸入徽章)。
默認的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文件時,可能會嵌入預覽或縮略圖(使用iOS11),並且當然會附加完全獨立的JPEG。
使用以上代碼, requestImage仍通過拉出其嵌入式JPEG來顯示DNG文件。 但是,它不知道PHAsset實際上是DNG文件。
如何確定PHAsset是否為DNG?
let fileExtension = ((asset.value(forKey: "uniformTypeIdentifier") as! NSString).pathExtension as NSString).uppercased
if fileExtension == "DNG" || fileExtension == "RAW-IMAGE" {
//Show RAW Badge
}
僅當DNG文件僅嵌入預覽JPEG時,以上方法才有效。 如果嵌入了常規的全尺寸JPEG,則它將PHAsset識別為JPEG。
有人告訴我試試這個:
let res = PHAssetResource.assetResources(for: asset)
但是某項資產可能具有多種資源(調整數據等)。 我將如何進行這項工作?
一些概念性背景:在PhotoKit中可以使用三個級別...
與PHAsset
和朋友一起工作時,您處於抽象模型級別。 每個資產都是“照片”數據庫中的一個條目-單個“事物”,在“照片”應用程序中顯示為縮略圖。 在這一層,它只是一個“事物”(不是像素緩沖區或視頻數據流)。
當您使用PHImageManager
,您仍在進行抽象工作。 您告訴PhotoKit,“給我一張圖像(或視頻), 這是在這種情況下向用戶顯示此資產的一種適當方法。” 在此級別仍會摘錄出哪種文件包含資產的原始數據。
這些抽象“事物”中的每一個都可能具有一個或多個原始文件,以提供其圖像或視頻數據,內部元數據等。要解決此類問題(包括文件格式),您需要使用PHAssetResource
(可能還包括PHAssetResourceManager
)。
因此,如果要查找資產是否包含RAW或DNG數據,則需要查看其資源。
使用PHAssetResource
.
assetResources(for:)
獲取與資產相對應的資源集。
通過檢查每種資源的type
屬性來縮小資源列表-由RAW或DNG文件支持的資產應將其存儲在alternatePhoto
類型的資源中。 (盡管第三方應用程序至少有某種可能性可以使用fullSizePhoto
類型寫入DNG文件,所以您也可以在fullSizePhoto
檢查。)
檢查縮小列表中每個資源的uniformTypeIdentifier
屬性。 DNG文件的UTI是"com.adobe.raw-image"
(在Xcode 9中,有一個用於此的字符串常量AVFileTypeDNG )。 如果只需要DNG文件,那可能就很好了,但是要更廣泛地檢查RAW文件,可能最好測試該資源的UTI是否符合"public.camera-raw-image"
aka kUTTypeRawImage
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.