1.0 简介
iOS8及以后苹果将操作相册的ALAssetsLibrary framework替换为Photos framework,在iOS9中废弃了原ALAssetsLibrary框架。 所以,如果应用支持7,8,9需要针对相册做不同版本的适配。
1.1 AssetsLibrary 简介
AssetsLibrary 的组成比较符合照片库本身的组成,照片库中的完整照片库对象、 相册、相片都能在 AssetsLibrary 中找到一一对应的组成,这使到 AssetsLibrary 的使用变得直观而方便。
AssetsLibrary: 代表整个设备中的资源库(照片库),通过 AssetsLibrary 可以获取和包括设备中的照片和视频
ALAssetsGroup: 映射照片库中的一个相册,通过 ALAssetsGroup 可以获取某个相册的信息,相册下的资源,同时也可以对某个相册添加资源。
ALAsset: 映射照片库中的一个照片或视频,通过 ALAsset 可以获取某个照片或视频的详细信息,或者保存照片和视频。
ALAssetRepresentation: ALAssetRepresentation 是对 ALAsset 的封装(但不是其子类),可以更方便地获取 ALAsset 中的资源信息,每个 ALAsset 都有至少有一个 ALAssetRepresentation 对象,可以通过 defaultRepresentation 获取。而例如使用系统相机应用拍摄的 RAW + JPEG 照片,则会有两个 ALAssetRepresentation,一个封装了照片的 RAW 信息,另一个则封装了照片的 JPEG 信息。
1.2 PhotoKit 简介
PhotoKit 是一套比 AssetsLibrary 更完整也更高效的库,对资源的处理跟 AssetsLibrary 也有很大的不同。
首先简单介绍几个概念:
PHAsset: 代表照片库中的一个资源,跟 ALAsset 类似,通过 PHAsset 可以获取和保存资源
PHFetchOptions: 获取资源时的参数,可以传 nil,即使用系统默认值
PHFetchResult: 表示一系列的资源集合
PHAssetCollection: 表示一个相册或者一个时刻
PHImageManager: 用于处理资源的加载,加载图片的过程带有缓存处理,可以通过传入一个 PHImageRequestOptions 控制资源的输出尺寸等规格
PHImageRequestOptions: 如上面所说,控制加载图片时的一系列参数
2.0 适配器模式进行适配
对于两个类所做的事情相同,但是具有不同接口的时候,适配器模式是一个很好的选择。
2.1适配单个资源(如图片,视频等单个资源).h文件
// LovePHAssetModel.h
// 适配对应PHAsset和ALAsset
// Created by 李晓光 on 16/7/27.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import
/**
* 资源媒体类型枚举,与PHAssetMediaType相对应
*/
typedef enum{
LovePHAssetMediaTypeUnknown = 0,
LovePHAssetMediaTypeImage = 1,
LovePHAssetMediaTypeVideo = 2,
LovePHAssetMediaTypeAudio = 3,
}LovePHAssetMediaType;
/**
* 上传资源压缩类型枚举,默认高清
*/
typedef enum{
//默认
LoveUploadCompressTypeDefault = 0,
//原图
LoveUploadCompressTypeOrigin = 1,
}LoveUploadCompressType;
@class PHAsset;
@class ALAsset;
@interface LovePHAssetModel : NSObject
/**
* 上传资源压缩类型
*/
@property (nonatomic, assign)LoveUploadCompressType compressType;
/**
* 媒体资源类型
*/
@property (nonatomic, assign)LovePHAssetMediaType mediaType;
/**
* 资源的创建时间
*/
@property (nonatomic, strong)NSDate *creationDate;
/**
* 资源的时长
*/
@property (nonatomic, assign)NSTimeInterval duration;
/**
* 资源大小
*/
@property (nonatomic, assign)long long assetSize;
/**
* Photos下对象
*/
@property (nonatomic,strong)PHAsset *phAsset;
/**
* AssetsLibrary下对象
*/
@property (nonatomic,strong)ALAsset *alAsset;
/**
* 同步获取缩略图
*
* @param resultHandler 图片
* @param photoWidth 照片宽度
* @return 信息
*/
- (void)get_synchronousThumbnailImageWithPhotoWidth:(CGFloat)photoWidth resultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler;
/**
* 异步获取缩略图
*
* @param resultHandler 图片
* @param photoWidth 照片宽度
* @return 信息
*/
- (void)get_asynchronousThumbnailImageWithPhotoWidth:(CGFloat)photoWidth resultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler;
/**
* 同步获取原图
*
* @param resultHandler 图片
*
* @return 信息
*/
- (void)get_synchronousOriginImageForResultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler;
/**
* 异步获取原图
*
* @param resultHandler 图片
*
* @return 信息
*/
- (void)get_asynchronousOriginImageForResultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler;
/**
* 获取资源图片的长宽
*
*/
- (CGSize)getDimension;
/**
* 获取原图data数据
*/
- (NSData *)getOriginData;
@end
2.2适配单个相册资源.h文件
ALAssetsLibrary框架下用ALAssetsGroup ,Photos下用PHAssetCollection与PHFetchResult组合表示
// LovePHAssetCollectionModel.h
// 功能:适配对应PHAssetCollection和ALAssetsGroup
// Created by 李晓光 on 16/7/26.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import
#import "LovePHAssetModel.h"
@class PHAssetCollection;
@class ALAssetsGroup;
@class PHFetchResult;
@interface LovePHAssetCollectionModel : NSObject
/**
* 资源数量
*/
@property (nonatomic, assign) NSUInteger estimatedAssetCount;
/**
* 相册标题
*/
@property (nonatomic, copy) NSString *localizedTitle;
/**
* Photos下对象
*/
@property (nonatomic, strong) PHAssetCollection *phAssetCollection;
/**
* 资源结果集合
*/
@property (nonatomic, strong) PHFetchResult *fetchResultAssets;
/**
* AssetsLibrary下对象
*/
@property (nonatomic, strong) ALAssetsGroup *alAssetsGroup;
/**
* 获取封面图片
*/
- (UIImage *)getAlbumCoverPhoto;
/**
* 获取单个资源
*
* @param assetBlock 单个资源模型回调
*/
- (void)getPHAssetModelForAssetBlock:(void (^)(LovePHAssetModel *phAssetModel))assetBlock;
@end
2.3获取相册资源列表.h文件
// LovePHAssetCollectionManager.h
// 功能:获取相册资源列表
// Created by 李晓光 on 16/7/26.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import
#import "LovePHAssetCollectionModel.h"
#import
@interface LovePHAssetCollectionManager : NSObject
+ (instancetype)sharedManager;
/**
* 获取相册资源
* @param mediaType 媒体资源类型
* @param assetBlock 相册资源模型回调
* @param failure 失败回调
*/
- (void)getAlbumPHAssetCollectionModelWithMediaType:(PHAssetMediaType)mediaType assetBlock:(void (^)(LovePHAssetCollectionModel *phAssetCollectionModel))assetBlock failure:(void (^)(NSError *error))failure;
@end
2.4获取相册资源列表.m文件
// LovePHAssetCollectionManager.m
// 功能:本地相册适配
// Created by 李晓光 on 16/7/26.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import "LovePHAssetCollectionManager.h"
#import
#import "GlobalUtils.h"
@interface LovePHAssetCollectionManager()
//
@property (nonatomic, strong)NSArray *assetCollectionSubtypes;
@end
@implementation LovePHAssetCollectionManager
//
+ (ALAssetsLibrary *)defaultAssetsLibrary
{
static dispatch_once_t pred = 0;
static ALAssetsLibrary *library = nil;
dispatch_once(&pred, ^{
library = [[ALAssetsLibrary alloc] init];
});
return library;
}
//
+ (instancetype)sharedManager
{
static LovePHAssetCollectionManager *sharedManager = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedManager = [[LovePHAssetCollectionManager alloc] init];
});
return sharedManager;
}
//
- (void)getAlbumPHAssetCollectionModelWithMediaType:(PHAssetMediaType)mediaType assetBlock:(void (^)(LovePHAssetCollectionModel *phAssetCollectionModel))assetBlock failure:(void (^)(NSError *error))failure;
{
if (isIOS8){
PHAuthorizationStatus author = [PHPhotoLibrary authorizationStatus];
if(author == PHAuthorizationStatusRestricted || author == PHAuthorizationStatusDenied){
failure([NSError errorWithDomain:@"无权限" code:PHAuthorizationStatusRestricted userInfo:nil]);
//无权限
}else{
//
PHFetchOptions *option = [[PHFetchOptions alloc] init];
if (isIOS9) {
option.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary;
}
if (mediaType != PHAssetMediaTypeUnknown) {
option.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", mediaType];
//
}
/*
PHAssetCollectionSubtypeSmartAlbumGeneric = 200,
PHAssetCollectionSubtypeSmartAlbumPanoramas = 201,
PHAssetCollectionSubtypeSmartAlbumVideos = 202,
PHAssetCollectionSubtypeSmartAlbumFavorites = 203,
PHAssetCollectionSubtypeSmartAlbumTimelapses = 204,
PHAssetCollectionSubtypeSmartAlbumAllHidden = 205,
PHAssetCollectionSubtypeSmartAlbumRecentlyAdded = 206,
PHAssetCollectionSubtypeSmartAlbumBursts = 207,
PHAssetCollectionSubtypeSmartAlbumSlomoVideos = 208,
PHAssetCollectionSubtypeSmartAlbumUserLibrary = 209,
*/
PHAssetCollectionSubtype smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumGeneric | PHAssetCollectionSubtypeSmartAlbumPanoramas | PHAssetCollectionSubtypeSmartAlbumVideos | PHAssetCollectionSubtypeSmartAlbumFavorites|PHAssetCollectionSubtypeSmartAlbumTimelapses|PHAssetCollectionSubtypeSmartAlbumAllHidden|PHAssetCollectionSubtypeSmartAlbumRecentlyAdded|PHAssetCollectionSubtypeSmartAlbumBursts|PHAssetCollectionSubtypeSmartAlbumSlomoVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary;
if (isIOS9) {
smartAlbumSubtype = PHAssetCollectionSubtypeSmartAlbumGeneric | PHAssetCollectionSubtypeSmartAlbumPanoramas | PHAssetCollectionSubtypeSmartAlbumVideos | PHAssetCollectionSubtypeSmartAlbumFavorites|PHAssetCollectionSubtypeSmartAlbumTimelapses|PHAssetCollectionSubtypeSmartAlbumAllHidden|PHAssetCollectionSubtypeSmartAlbumRecentlyAdded|PHAssetCollectionSubtypeSmartAlbumBursts|PHAssetCollectionSubtypeSmartAlbumSlomoVideos|PHAssetCollectionSubtypeSmartAlbumUserLibrary|PHAssetCollectionSubtypeSmartAlbumSelfPortraits|PHAssetCollectionSubtypeSmartAlbumScreenshots;
}
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:smartAlbumSubtype options:nil];
for (PHAssetCollection *assetCollection in smartAlbums) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
LovePHAssetCollectionModel *model = [[LovePHAssetCollectionModel alloc] init];
model.phAssetCollection = assetCollection;
model.fetchResultAssets = fetchResult;
assetBlock(model);
}
PHFetchResult *albums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular | PHAssetCollectionSubtypeAlbumSyncedAlbum|PHAssetCollectionSubtypeAlbumSyncedFaces options:nil];
for (PHAssetCollection *assetCollection in albums) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
LovePHAssetCollectionModel *model = [[LovePHAssetCollectionModel alloc] init];
model.phAssetCollection = assetCollection;
model.fetchResultAssets = fetchResult;
assetBlock(model);
}
//结束
assetBlock(nil);
}
}else{
ALAssetsLibraryGroupsEnumerationResultsBlock resultsBlock = ^(ALAssetsGroup *group, BOOL *stop) {
if (group)
{
switch (mediaType) {
case PHAssetMediaTypeImage:{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
break;
}
case PHAssetMediaTypeVideo:{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
break;
}
default:{
[group setAssetsFilter:[ALAssetsFilter allAssets]];
break;
}
}
LovePHAssetCollectionModel *model = [[LovePHAssetCollectionModel alloc] init];
model.alAssetsGroup = group;
assetBlock(model);
}else{
assetBlock(nil);
}
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {
failure(error);
};
// Enumerate Camera roll first
[[LovePHAssetCollectionManager defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:resultsBlock
failureBlock:failureBlock];
NSUInteger type =
ALAssetsGroupLibrary | ALAssetsGroupAlbum | ALAssetsGroupEvent |
ALAssetsGroupFaces | ALAssetsGroupPhotoStream;
[[LovePHAssetCollectionManager defaultAssetsLibrary] enumerateGroupsWithTypes:type
usingBlock:resultsBlock
failureBlock:failureBlock];
}
}
@end
2.5适配单个相册资源.m文件
// LovePHAssetCollectionModel.m
// 功能:适配对应PHAssetCollection和ALAssetsGroup
// Created by 李晓光 on 16/7/26.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import "LovePHAssetCollectionModel.h"
#import
#import
@interface LovePHAssetCollectionModel()
@end
@implementation LovePHAssetCollectionModel
#pragma mark - ALAssetsLibrary
- (UIImage *)getAlbumCoverPhoto{
UIImage *coverImage = nil;
if (_phAssetCollection) {
PHFetchResult *groupResult = [PHAsset fetchAssetsInAssetCollection:self.phAssetCollection options:nil];
PHAsset *asset = groupResult.lastObject;
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat dimension = 120.0f;
CGSize size = CGSizeMake(dimension * scale, dimension * scale);
__block UIImage *resultImage = nil;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:requestOptions resultHandler:^(UIImage *result, NSDictionary *info) {
resultImage = result;
}];
coverImage = resultImage;
}else if(_alAssetsGroup){
CGImageRef posterImage = _alAssetsGroup.posterImage;
size_t height = CGImageGetHeight(posterImage);
float scale = height / 65;
coverImage = [UIImage imageWithCGImage:posterImage scale:scale orientation:UIImageOrientationUp];
}
return coverImage;
}
-(NSUInteger)estimatedAssetCount
{
if (_estimatedAssetCount == 0) {
if (_phAssetCollection) {
if (_fetchResultAssets) {
_estimatedAssetCount = [_fetchResultAssets count];
}else{
_estimatedAssetCount = _phAssetCollection.estimatedAssetCount;
}
}else if(_alAssetsGroup){
_estimatedAssetCount = [_alAssetsGroup numberOfAssets];
}
}
return _estimatedAssetCount;
}
//
-(NSString *)localizedTitle
{
if (!_localizedTitle) {
if (_phAssetCollection) {
_localizedTitle = _phAssetCollection.localizedTitle;
}else if(_alAssetsGroup){
_localizedTitle = [_alAssetsGroup valueForProperty:ALAssetsGroupPropertyName];
}
}
return _localizedTitle;
}
//
- (void)getPHAssetModelForAssetBlock:(void (^)(LovePHAssetModel *phAssetModel))assetBlock
{
if (_phAssetCollection) {
if (_fetchResultAssets) {
for (PHAsset *asset in _fetchResultAssets) {
LovePHAssetModel *phAssetModel = [[LovePHAssetModel alloc] init];
phAssetModel.phAsset = asset;
phAssetModel.mediaType = asset.mediaType;
assetBlock(phAssetModel);
}
}else{
PHFetchResult *assets = [PHAsset fetchKeyAssetsInAssetCollection:_phAssetCollection options:nil];
for (PHAsset *asset in assets) {
LovePHAssetModel *phAssetModel = [[LovePHAssetModel alloc] init];
phAssetModel.phAsset = asset;
phAssetModel.mediaType = asset.mediaType;
assetBlock(phAssetModel);
}
_fetchResultAssets = assets;
}
//结束
assetBlock(nil);
}else if(_alAssetsGroup){
ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset)
{
LovePHAssetModel *phAssetModel = [[LovePHAssetModel alloc] init];
phAssetModel.alAsset = asset;
NSString *type = [asset valueForProperty:ALAssetPropertyType];
if ([type isEqual:ALAssetTypePhoto]){
phAssetModel.mediaType = LovePHAssetMediaTypeImage;
}else if ([type isEqual:ALAssetTypeVideo]){
phAssetModel.mediaType = LovePHAssetMediaTypeVideo;
}
assetBlock(phAssetModel);
}else{
assetBlock(nil);
}
};
[self.alAssetsGroup enumerateAssetsUsingBlock:resultsBlock];
}
}
//
@end
2.6适配单个资源(如图片,视频等单个资源).m文件
//
// LovePHAssetModel.m
// 适配对应PHAsset和ALAsset
// Created by 李晓光 on 16/7/27.
// Copyright © 2016年 Discoverybay. All rights reserved.
//
#import "LovePHAssetModel.h"
#import
#import
@interface LovePHAssetModel()
@end
@implementation LovePHAssetModel
#pragma mark ================缩略图===============
- (void)get_asynchronousThumbnailImageWithPhotoWidth:(CGFloat)photoWidth resultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler
{
[self getThumbnailImageIsSynchronous:NO photoWidth:photoWidth ResultHandler:resultHandler];
}
- (void)get_synchronousThumbnailImageWithPhotoWidth:(CGFloat)photoWidth resultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler
{
[self getThumbnailImageIsSynchronous:YES photoWidth:photoWidth ResultHandler:resultHandler];
}
- (void)getThumbnailImageIsSynchronous:(BOOL)isSynchronous photoWidth:(CGFloat)photoWidth ResultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler
{
if (_phAsset) {
//
PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
if (isSynchronous) {
phImageRequestOptions.synchronous = YES;
}else{
phImageRequestOptions = nil;
}
CGFloat aspectRatio = _phAsset.pixelWidth / (CGFloat)_phAsset.pixelHeight;
CGFloat multiple = [UIScreen mainScreen].scale;
CGFloat pixelWidth = photoWidth * multiple;
CGFloat pixelHeight = pixelWidth / aspectRatio;
[[PHImageManager defaultManager] requestImageForAsset:_phAsset targetSize:CGSizeMake(pixelWidth, pixelHeight) contentMode:PHImageContentModeAspectFill options:phImageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
resultHandler(result,info);
}];
}else if (_alAsset){
resultHandler([UIImage imageWithCGImage:_alAsset.thumbnail],nil);
}
}
#pragma mark ===============原图=========================
- (void)get_synchronousOriginImageForResultHandler:(void (^)(UIImage* result, NSDictionary * info))resultHandler
{
[self getOriginImageIsSynchronous:YES ResultHandler:resultHandler];
}
- (void)get_asynchronousOriginImageForResultHandler:(void (^)(UIImage *result, NSDictionary * info))resultHandler
{
[self getOriginImageIsSynchronous:NO ResultHandler:resultHandler];
}
- (void)getOriginImageIsSynchronous:(BOOL)isSynchronous ResultHandler:(void (^)(UIImage * result, NSDictionary * info))resultHandler
{
if (_phAsset) {
PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
if (isSynchronous) {
phImageRequestOptions.synchronous = YES;
}else{
phImageRequestOptions = nil;
}
[[PHImageManager defaultManager] requestImageForAsset:_phAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:phImageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
resultHandler(result,info);
}];
}else if (_alAsset){
ALAssetRepresentation *rep = [_alAsset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
resultHandler([UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]],nil);
}else{
resultHandler(nil,nil);
}
}
}
/**
* 获取资源的长宽size
*
*/
- (CGSize)getDimension
{
CGSize size = CGSizeMake(0, 0);
if (_phAsset) {
//
size = CGSizeMake(_phAsset.pixelWidth, _phAsset.pixelHeight);
}else if(_alAsset){
ALAssetRepresentation* representation = [_alAsset defaultRepresentation];
size = [representation dimensions];
}
return size;
}
/**
* 获取资源容量大小
*
*/
-(long long)assetSize
{
if (!_assetSize) {
if (_phAsset) {
//
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:_phAsset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
_assetSize = [(NSData *)info[@"PHImageFileDataKey"] length];
}];
}else if (_alAsset){
ALAssetRepresentation* representation = [_alAsset defaultRepresentation];
_assetSize = [representation size];
}
}
return _assetSize;
}
/**
* 获取原图data数据
*/
- (NSData *)getOriginData
{
__block NSData *data = nil;
if (_phAsset) {
//
PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
phImageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:_phAsset options:phImageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
data = imageData;
}];
}else if (_alAsset){
ALAssetRepresentation *rep = [_alAsset defaultRepresentation];
//注意这里丢失了精度.不过目前来看不可能出现问题 size不可能到达long long的精度
NSUInteger repSize = [rep size];
uint8_t *buffer = malloc(sizeof(uint8_t) * repSize);
if (!buffer) {
//照片处理失败.不发送
return nil;
}
NSError *error = nil;
NSUInteger retV = [rep getBytes:buffer fromOffset:0 length:repSize error:&error];
if (retV == 0 || error != nil) {
//出错就返回.
free(buffer);
return nil;
}
data = [NSData dataWithBytes:buffer length:repSize];
//销毁
free(buffer);
}
return data;
}
-(NSTimeInterval)duration
{
if (!_duration) {
if (_phAsset) {
_duration = _phAsset.duration;
}else if (_alAsset){
_duration = [[_alAsset valueForProperty:ALAssetPropertyDuration] doubleValue];
}
}
return _duration;
}
-(NSDate *)creationDate
{
if (!_creationDate) {
if (_phAsset) {
_creationDate = _phAsset.creationDate;
}else if (_alAsset){
_creationDate = [_alAsset valueForProperty:ALAssetPropertyDate];
}
}
return _creationDate;
}
@end
3.0总结
1 在 AssetsLibrary 中,相册和资源是分别对应ALAssetGroup 和 ALAsset,能直接获取相关信息。 PhotoKit 则是通过PHFetchResult 筛选的资源合集。
2 本文适配的核心思想就是构建一个新类,AssetsLibrary,PhotoKit下操作所对应的类,分别为这个新类的属性。对新类定义统一接口进行相应逻辑操作。