簡體   English   中英

有沒有辦法獲取我的iOS應用屏幕截圖的列表?

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

在我的iOS應用程序中,是否可以訪問從我的應用程序獲取的所有屏幕截圖?

如果不可能,是否有辦法僅使用屏幕快照相冊(來自所有應用程序)打開圖像選擇器控制器(UIImagePickerController)?

謝謝。

除了此處定義的源代碼外,沒有其他方法可以顯示標准的UIImagePickerController源類型

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

但是,有一種方法可以截取屏幕快照的相冊並將其顯示在自己的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
        }];
    }];
}

問題是最后一個屏幕截圖(觸發通知的屏幕截圖)在執行代碼時尚不可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM