简体   繁体   English

有没有办法获取我的iOS应用屏幕截图的列表?

[英]Is there a way to get the list of my iOS app screenshots?

is there a way, in my iOS app, to access to all the screenshots taken from my app ? 在我的iOS应用程序中,是否可以访问从我的应用程序获取的所有屏幕截图?

If not possible, is there a way to open the image picker controller (UIImagePickerController) with only the screenshot album (from all apps) ? 如果不可能,是否有办法仅使用屏幕快照相册(来自所有应用程序)打开图像选择器控制器(UIImagePickerController)?

Thank you. 谢谢。

There's no way to present standard UIImagePickerController with source type other than defined here 除了此处定义的源代码外,没有其他方法可以显示标准的UIImagePickerController源类型

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/#//apple_ref/c/tdef/UIImagePickerControllerSourceType https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImagePickerController_Class/#//apple_ref/c/tdef/UIImagePickerControllerSourceType

However, there is a way to grab screenshots' album and present it in own UI. 但是,有一种方法可以截取屏幕快照的相册并将其显示在自己的UI中。 According to documentation you can do something like this: 根据文档,您可以执行以下操作:

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "localizedTitle = Screenshots")
let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: options)
let sceenShots = collections.firstObject as? PHAssetCollection

but due to bug (above will crash because of predicate) you can fetch all albums and then filter for screenshots' album (works for iOS8+) 但由于存在错误(上述操作由于谓词而崩溃),因此您可以提取所有相册,然后过滤屏幕快照的相册(适用于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
    }
}

Or If you target for iOS9+ you can do like this: 或者,如果您的目标是iOS9 +,则可以执行以下操作:

let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumScreenshots, options: nil)
let screenshots = collections.lastObject as? PHAssetCollection

Please also keep in mind, that it's not possible to grab screenshots from specific app. 另外请记住,不可能从特定应用程序中截取屏幕截图。

I have also considered a different way to access to all the screenshots taken from my app. 我还考虑了一种不同的方式来访问从我的应用程序获取的所有屏幕截图。 The idea is to intercept the screenshot with the UIApplicationUserDidTakeScreenshotNotification and then retrieve and save the file URL (or copy the file) : 这个想法是使用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
        }];
    }];
}

The problem is the last screenshot, the one that triggered the notification, is not yet available at the time of the code is executed. 问题是最后一个屏幕截图(触发通知的屏幕截图)在执行代码时尚不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM