2016-02-26 80 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 
     }]; 
    }]; 
} 

的问题是最后的截图,触发了一个通知,在代码执行时尚不可用。

相关问题