按关键字搜索 iOS 照片?

Posted

技术标签:

【中文标题】按关键字搜索 iOS 照片?【英文标题】:Search iOS photos by keyword? 【发布时间】:2020-02-07 04:04:24 【问题描述】:

Apple 的照片应用程序允许用户使用“冲浪”、“食物”、“天空”等搜索关键字查询照片。

具有相机和照片权限的第三方 ios 应用如何使用任意字符串搜索手机的相机胶卷?

Searching for Photos is part of the SiriKit API。我们可以传递字符串作为一种可能的方法还是有更好的方法?

【问题讨论】:

【参考方案1】:

即使经过更长时间的研究,我也找不到方法。但好消息是,您可以使用与在照片应用程序中实现结果完全相同的神经网络来构建您自己的搜索索引。下面是一些示例代码:

let fetchOptions = PHFetchOptions()           
let fetchResult = PHAsset.fetchAssets(with: fetchOptions)
fetchResult.enumerateObjects  asset, index, pointer in
    self.assetManager.requestImage(for: asset,
                                   targetSize: CGSize(width: 150, height: 100), 
                                   contentMode: .aspectFit, 
                                   options: .none)  image, metadata in
            if let uiImage = image, let cgImage = uiImage.cgImage 
                let requestHandler = VNImageRequestHandler(cgImage: cgImage)
                let request = VNClassifyImageRequest()
                try? requestHandler.perform([request])
                let results = request.results as! [VNClassificationObservation]
                // Filter the 1303 classification results, and use them in your app
                // in my case, fullfill promise with a wrapper object
                promise(.success(ClassifiedImage(imageIdentifier: asset.localIdentifier,
                                classifications: results.filter  $0.hasMinimumPrecision(0.9, forRecall: 0.0) .map
                                    ($0.identifier, nil)
                                    )))
            

        

更多信息:https://developer.apple.com/videos/play/wwdc2019/222

另请注意:在构建搜索索引时,您可以在获取资产时使用 PHAsset 类的 localIdentifier

【讨论】:

以上是关于按关键字搜索 iOS 照片?的主要内容,如果未能解决你的问题,请参考以下文章

PHP中文件名的关键字搜索

Django ORM使用子查询按关键字搜索文本

如何简化在字符串中搜索关键字并按相关性排序的 LINQ 查询?

javascript 从Google Analytics界面获取按频率排序的各个自然搜索关键字列表

IOS_SearchBar搜索栏及关键字高亮

微信搜索新发现:iPhone 内存不足看这里!