有没有办法在我的iOS应用程序中访问从我的应用程序中获取的所有屏幕截图?有没有办法让我的iOS应用程序截图列表?
如果不可能,有没有办法打开图像选择控制器(UIImagePickerController)只有截图专辑(从所有应用程序)?
谢谢。
有没有办法在我的iOS应用程序中访问从我的应用程序中获取的所有屏幕截图?有没有办法让我的iOS应用程序截图列表?
如果不可能,有没有办法打开图像选择控制器(UIImagePickerController)只有截图专辑(从所有应用程序)?
谢谢。
有比这里定义
但是没有办法给出标准的UIImagePickerController与其他来源的类型,有一种方法可以抢截图专辑,在自己的UI呈现它。根据文档,你可以做这样的事情:
let options = PHFetchOptions()
options.predicate = NSPredicate(format: "localizedTitle = Screenshots")
let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: options)
let sceenShots = collections.firstObject as? PHAssetCollection
但由于错误(上面会崩溃,因为谓语),您可以获取所有专辑中,然后过滤截图专辑(适用于iOS8上+)
let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: nil)
var screenshots: PHAssetCollection?
collections.enumerateObjectsUsingBlock {
(collection, _, _) -> Void in
if collection.localizedTitle == "Screenshots" {
screenshots = collection as? PHAssetCollection
}
}
,或者如果你的目标为iOS9 +,你可以这样做:
let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumScreenshots, options: nil)
let screenshots = collections.lastObject as? PHAssetCollection
也请记住,这是不可能抓住从特定应用程序的截图。
我也考虑过不同的方式来访问从我的应用程序采取的所有屏幕截图。我们的想法是与UIApplicationUserDidTakeScreenshotNotification截取屏幕截图,然后检索并保存文件URL(或复制文件):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenshotDetected) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
- (void)screenshotDetected {
PHFetchResult<PHAssetCollection *> *albums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumScreenshots options:nil];
[albums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull album, NSUInteger idx, BOOL * _Nonnull stop) {
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.wantsIncrementalChangeDetails = YES;
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d",PHAssetMediaTypeImage];
PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:album options:options];
[assets enumerateObjectsUsingBlock:^(PHAsset * _Nonnull asset, NSUInteger idx, BOOL * _Nonnull stop) {
// do things
}];
}];
}
的问题是最后的截图,触发了一个通知,在代码执行时尚不可用。
不要比较集合的localizedTitle。查看该集合的'assetCollectionSubtype'并查看它是否为'SmartAlbumScreenshots'。 – rmaddy
顺便说一句 - 这会获取所有截图,而不仅仅是从特定应用中获取的截图。 – rmaddy
@maddy谢谢,我不知道为什么我错过了。 – Kubba