2016-02-26 50 views

回答

5

有比這裏定義

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

但是沒有辦法給出標準的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 

也請記住,這是不可能抓住從特定應用程序的截圖。

+3

不要比較集合的localizedTitle。查看該集合的'assetCollectionSubtype'並查看它是否爲'SmartAlbumScreenshots'。 – rmaddy

+0

順便說一句 - 這會獲取所有截圖,而不僅僅是從特定應用中獲取的截圖。 – rmaddy

+0

@maddy謝謝,我不知道爲什麼我錯過了。 – Kubba

0

我也考慮過不同的方式來訪問從我的應用程序採取的所有屏幕截圖。我們的想法是與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 
     }]; 
    }]; 
} 

的問題是最後的截圖,觸發了一個通知,在代碼執行時尚不可用。

相關問題