iOS:PHAsset和检测DNG RAW格式

Giz*_*odo 0 xcode ios dng uicollectionview phasset

我得到了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

    }
Run Code Online (Sandbox Code Playgroud)

DNG / RAW格式

使用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
}
Run Code Online (Sandbox Code Playgroud)

仅当DNG文件仅嵌入预览JPEG时,以上方法才有效。如果嵌入了常规的全尺寸JPEG,则它将PHAsset识别为JPEG。

有人告诉我试试这个:

let res = PHAssetResource.assetResources(for: asset)
Run Code Online (Sandbox Code Playgroud)

但是某项资产可能具有多种资源(调整数据等)。我将如何进行这项工作?

ric*_*ter 5

一些概念性背景:在PhotoKit中可以使用三个级别...

  • PHAsset与朋友一起工作时,您处于抽象模型级别。每个资产都是“照片”数据库中的一个条目-单个“事物”,在“照片”应用程序中显示为缩略图。在这一层,它只是一个“事物”(不是像素缓冲区或视频数据流)。

  • 当您使用时PHImageManager,您仍然有点抽象。您告诉PhotoKit,“给我一张图像(或视频),是在这种情况下向用户显示此资产的一种适当方法。” 在此级别仍会摘录出哪种文件包含资产的原始数据。

  • 这些抽象的“事物”中的每一个都可能具有一个或多个原始文件,以提供其图像或视频数据,内部元数据等。要解决此类问题(包括文件格式),您需要PHAssetResource(可能需要PHAssetResourceManager)进行处理。

因此,如果要查找资产是否包含RAW或DNG数据,则需要查看其资源。

  1. 使用来获得相对应于资产资源。PHAssetResource.assetResources(for:)

  2. 通过检查type每种资源的属性来缩小资源列表-由RAW或DNG文件支持的资产应将其存储在alternatePhoto类型的资源中。(尽管第三方应用程序至少有某种可能性可以使用该fullSizePhoto类型写入DNG文件,所以您也可以在其中进行检查。)

  3. 检查uniformTypeIdentifier缩小列表中每个资源的属性。DNG文件的UTI是"com.adobe.raw-image"(在Xcode 9中有一个字符串常量AVFileTypeDNG)。如果只需要DNG文件,那可能很好,但是要更广泛地检查RAW文件,最好测试该资源的UTI是否符合"public.camera-raw-image"akakUTTypeRawImage