0. 截屏广播 UIApplicationUserDidTakeScreenshotNotification
1. iOS9.0以下,获取系统相册最后一张照片
//#import <AssetsLibrary/AssetsLibrary.h>
//NS_DEPRECATED_IOS(4_0, 9_0, "Use UIImageOrientation in the Photos framework instead");
AssetsLibrary 的组成比较符合照片库本身的组成,照片库中的完整照片库对象、相册、相片都能在 AssetsLibrary 中找到一一对应的组成,这使到 AssetsLibrary 的使用变得直观而方便。
- (void)latestAsset:(void (^)(ALAsset * _Nullable, NSError *_Nullable))block {
[self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse/*遍历方式-反向*/ usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
ALAssetRepresentation *assetRep = [result defaultRepresentation];
CGImageRef imgRef = [assetRep fullResolutionImage];
UIImage *image = [[UIImage alloc] initWithCGImage:imgRef];
*stop = YES;
}
}];
*stop = YES;
}
} failureBlock:^(NSError *error) {
if (error) {
}
}];
}
2.iOS8以上可以使用 #import <Photos/Photos.h> 来获取系统的照片
// 列出所有相册智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:
nil
];
// 列出所有用户创建的相册
PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:
nil
];
// 获取所有资源的集合,并按资源的创建时间排序
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[
NSSortDescriptor
sortDescriptorWithKey:
@"creationDate"
ascending:
YES
]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
// 在资源的集合中获取第一个集合,并获取其中的图片
PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
PHAsset *asset = assetsFetchResults[0];
[imageManager requestImageForAsset:asset
targetSize:SomeSize
contentMode:PHImageContentModeAspectFill
options:
nil
resultHandler:^(UIImage *result,
NSDictionary
*info) {
// 得到一张 UIImage,展示到界面上
}];
- (void)latestAsset
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
PHAsset *phasset = [assetsFetchResults lastObject];
PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
[imageManager requestImageForAsset:phasset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
}];
}