我得到了苹果的'SamplePhotosApp‘示例代码,并在相册的网格照片布局中,试图检测DNG原始文件(如果是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/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
}以上只在DNG文件只有预览JPEG嵌入的情况下起作用。如果它有一个常规的全尺寸JPEG嵌入,它会识别PHAsset为JPEG.
有人让我试试这个:
let res = PHAssetResource.assetResources(for: asset)但某一资产可能有若干资源(调整数据等)。我怎么能做到这一点呢?
发布于 2017-09-25 21:45:44
概念背景:在PhotoKit中有三个层次可以工作.
PHAsset和朋友一起工作时,您处于抽象的模型级别。每个资产都是Photos数据库中的一个条目--一个在Photos应用程序中以缩略图形式出现的“东西”。在这一层,它只是一个“东西”(比如说,不是像素缓冲区或视频数据流)。PHImageManager时,您仍然在抽象地工作。您告诉PhotoKit,“给我一个图像(或视频),这是在这种情况下向用户显示此资产的适当方式。”在这个级别上,包含资产原始数据的文件类型仍然是抽象的。PHAssetResource (可能还有PHAssetResourceManager)。因此,如果要查找资产是包含原始数据还是包含DNG数据,则需要查看其资源。
PHAssetResource.assetResources(for:)获取与资产对应的一组资源。type属性来缩小资源列表--由RAW或DNG文件支持的资产应该将其存储在alternatePhoto类型的资源中。(虽然至少有一些第三方应用程序可以使用fullSizePhoto类型编写DNG文件,所以您也可以检查一下。)uniformTypeIdentifier属性。DNG文件的UTI是"com.adobe.raw-image" (在Xcode 9中有一个字符串常量,AVFileTypeDNG)。如果您只想要DNG文件,这可能很好,但是为了更广泛地检查原始文件,最好测试资源的UTI是否符合kUTTypeRawImage。https://stackoverflow.com/questions/46409498
复制相似问题