PhotoKit框架介绍及使用

  PhotoKit 是一套比 AssetsLibrary 更新更完整也更高效的ios照片处理库,对资源的处理跟 AssetsLibrary 有很大的不同。下面简单介绍下PhotoKit的几个基本概念

  • PHAsset:代表照片库中的特定资源,PHAsset对象来表示存储在照片应用程序中或云端的一个图片或视频文件。为了显示或编辑asset,我们需要使用PHAsset类的获取asset来处理它,一个asset对象是不可变的,只包含它所代表的照片或视频的元数据。
  • PHFetchOptions:使用PHFetchOptions对象来指定选项,在PHAsset, PHCollection, PHAssetCollection, and PHCollectionList上根据它的对象来检索照片符合要求的实体。我们可以根据一些key来对要显示的照片进行排序。下面是支持的keyPhotoKit框架介绍及使用
  • PHFetchResult: 表示一系列的资源集合,也可以是相册的集合。它是一个有序的存储照片实体的容器。它是一个泛型集合
  • PHAssetCollection: 表示一个相册
  • PHImageManager: 用于处理资源的加载,加载图片的过程带有缓存处理,可以通过传入一个 PHImageRequestOptions 控制资源的输出尺寸等规格
  • PHImageRequestOptions: 控制加载图片时的一系列参数

下面详细介绍库的使用

我们需要导入Photos.framework

1.PHPhotoLibrary:表示用户的照片库,包括对象存储在本地设备和(如果已启用)在iCloud照片。我们使用PHPhotoLibrary来对照片库进行操作。可以添加编辑删除。

例如使用PHPhotoLibrary来实现拍照功能。

    [_mCamera capturePhotoAsJPEGProcessedUpToFilter:_mFilter withCompletionHandler:^(NSData *processedJPEG, NSError *error){

        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];

        } completionHandler:^(BOOL success, NSError * _Nullable error) {

        }];

    }];

2.PHAssetChangeRequest,PHAssetCollectionChangeRequest,PHAssetCreationRequest,PHCollectionListChangeRequest:你可以在PHPhotoLibrary的change block中创建一个它们的对象来对asset对象进行添加,编辑,删除

在上面那段代码中我们展现了PHAssetCreationRequest的添加操作。

[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:processedJPEG options:nil];

3.PHAssetCollectionType相册类型

typedef NS_ENUM(NSInteger, PHAssetCollectionType) {
PHAssetCollectionTypeAlbum = ,
PHAssetCollectionTypeSmartAlbum = ,
PHAssetCollectionTypeMoment = ,
} NS_ENUM_AVAILABLE_IOS(8_0);

[PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]

输出:

-- ::59.392 FWLifeApp[:] Favorites

-- ::59.394 FWLifeApp[:] Recently Deleted

-- ::59.394 FWLifeApp[:] Panoramas

-- ::59.394 FWLifeApp[:] Camera Roll

-- ::59.396 FWLifeApp[:] Slo-mo

-- ::59.396 FWLifeApp[:] Screenshots

-- ::59.397 FWLifeApp[:] Bursts

-- ::59.398 FWLifeApp[:] Videos

-- ::59.398 FWLifeApp[:] Selfies

-- ::59.399 FWLifeApp[:] Hidden

-- ::59.400 FWLifeApp[:] Time-lapse

-- ::59.400 FWLifeApp[:] Recently Added

我们可以看出PHAssetCollectionTypeSmartAlbum包含许多相册集。收藏,最近删除,最近添加,相机胶卷等,而不包含时刻.

NSMutableArray<FWPhotoAlbums *> *mArr = [NSMutableArray array];
PHFetchResult *smartAlbum = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbum enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//过滤掉最近删除的,和视频
if (obj.assetCollectionSubtype != && obj.assetCollectionSubtype < ) {
NSArray <PHAsset *> *assets = [self getAssetsInAssetCollection:obj ascending:NO];
if ([assets count]) {
FWPhotoAlbums *pa = [[FWPhotoAlbums alloc] init];
pa.albumName = [self TitleOfAlbumForChinse:obj.localizedTitle];
pa.albumImageCount = [assets count];
pa.firstImageAsset = assets.firstObject;
pa.assetCollection = obj;
[mArr addObject:pa];
}
}
}];

我们需要进一步获取每个相册集下的所有照片。对照片的creationDate key进行排序

- (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options]; return result;
} - (NSArray <PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
NSMutableArray <PHAsset *> *mArr = [NSMutableArray array];
PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];
[result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.mediaType == PHAssetMediaTypeImage) {
[mArr addObject:obj];
}
}]; return mArr;
}

获取图片

- (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion
{
static PHImageRequestID requestID = -;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = MIN(WIDTH, );
if (requestID >= && size.width/width==scale) {
[[PHCachingImageManager defaultManager] cancelImageRequest:requestID];
} PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = resizeMode;
option.networkAccessAllowed = YES; requestID = [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey];
if (downloadFinined && completion) {
completion(image, info);
}
}];
}
上一篇:iOS学习笔记--触摸事件


下一篇:Centos6.6部署Redis集群