You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Photo Kit筛选:排除截图、录屏及其他应用保存的媒体资源

iOS Photo Kit 媒体资源过滤方案

1. 录屏资源高效筛选

直接利用PHAsset的媒体子类型PHAssetMediaSubtypeVideoScreenRecording进行过滤,无需逐个解析资源,性能最优:

let fetchOptions = PHFetchOptions()
// 排除录屏视频
fetchOptions.predicate = NSPredicate(format: "mediaType = %d AND mediaSubtype != %d", 
                                     PHAssetMediaType.video.rawValue, 
                                     PHAssetMediaSubtype.videoScreenRecording.rawValue)
let nonScreenRecordings = PHAsset.fetchAssets(with: fetchOptions)

这个方案和你筛选截图的逻辑一致,系统已为录屏资源标记了专属子类型,完全避免了PHAssetResource的高开销操作,是官方推荐的高效方式。

2. 其他应用保存资源的筛选

iOS 15及以上版本,可通过PHAssetResource的sourceApplicationIdentifier属性获取保存资源的应用Bundle ID,以此判断是否为其他应用保存的内容:

  • 原生相机拍摄的资源,sourceApplicationIdentifier为com.apple.camera
  • 其他应用保存的资源,会对应其自身的Bundle ID

批量处理时可通过异步接口优化性能,示例代码:

let assets = PHAsset.fetchAssets(with: .image, options: nil)
assets.enumerateObjects { asset, _, _ in
    let resources = PHAssetResource.assetResources(for: asset)
    for resource in resources {
        if let appID = resource.sourceApplicationIdentifier, appID != "com.apple.camera" {
            // 该资源由第三方应用保存,执行排除逻辑
            continue
        }
    }
}

若需兼容iOS 14及以下,可通过PHAsset的originalFilename辅助判断(比如部分应用会在文件名中加入标识),但准确性略低,优先推荐iOS 15+的API方案。

注:原生相册中显示的“由XX保存”信息,正是基于sourceApplicationIdentifier映射的应用名称。

内容的提问来源于stack exchange,提问作者Mev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:50:21