源码下载:https://github.com/darling0825/DLPhotoPicker.git
1.相册权限获取
AssetsLibrary:
- (void)checkAuthorizationStatus_BeforeiOS8
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status)
{
case ALAuthorizationStatusNotDetermined:
{
[self requestAuthorizationStatus_AfteriOS8];
break;
}
case ALAuthorizationStatusRestricted:
case ALAuthorizationStatusDenied:
{
[self showAccessDenied];
break;
}
case ALAuthorizationStatusAuthorized:
default:
{
[self checkAuthorizationSuccess];
break;
}
}
}
Photos:
- (void)checkAuthorizationStatus_AfteriOS8
{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status)
{
case PHAuthorizationStatusNotDetermined:
{
[self requestAuthorizationStatus_AfteriOS8];
break;
}
case PHAuthorizationStatusRestricted:
case PHAuthorizationStatusDenied:
{
[self showAccessDenied];
break;
}
case PHAuthorizationStatusAuthorized:
default:
{
[self checkAuthorizationSuccess];
break;
}
}
}
- (void)requestAuthorizationStatus_AfteriOS8
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
dispatch_async(dispatch_get_main_queue(), ^{
switch (status) {
case PHAuthorizationStatusAuthorized:
{
[self checkAuthorizationSuccess];
break;
}
default:
{
[self showAccessDenied];
break;
}
}
});
}];
}
2.获取相册
AssetsLibrary:
- (void)getAlbumsFromDevice_BeforeiOS8
{
NSMutableArray *albumsArray = [[NSMutableArray alloc] init];
__weak typeof(self) weakSelf = self;
[_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (group) {
/**
* allPhotos: Get all photos assets in the assets group.
* allVideos: Get all video assets in the assets group.
* allAssets: Get all assets in the group.
*/
[group setAssetsFilter:[ALAssetsFilter allAssets]];
if (!strongSelf.showsEmptyAlbums && group.numberOfAssets <= 0) {
;
}else{
DLPhotoCollection *collection = [[DLPhotoCollection alloc] initWithAssetCollection:group];
NSInteger groupType = [[group valueForProperty:ALAssetsGroupPropertyType] integerValue];
if(groupType == ALAssetsGroupSavedPhotos){
[albumsArray insertObject:collection atIndex:0];
}
else{
[albumsArray addObject:collection];
}
}
} else {
if ([albumsArray count] > 0) {
strongSelf.photoCollections = [NSMutableArray arrayWithArray:albumsArray];
// 把所有的相册储存完毕,可以展示相册列表
[strongSelf getAlbumsCompletion:YES];
} else {
// 没有任何有资源的相册,输出提示
[strongSelf showNoAssets];
}
}
} failureBlock:^(NSError *error) {
NSLog(@">>>Asset group not found!\n");
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf getAlbumsCompletion:NO];
}];
}
Photos
- (void)getAlbumsFromDevice
{
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:self.assetsFetchOptions];
if (fetchResult.count <= 0) {
[self showNoAssets];
return;
}
NSMutableArray *fetchResults = [NSMutableArray new];
for (NSNumber *subtypeNumber in self.assetCollectionSubtypes)
{
PHAssetCollectionSubtype subtype = subtypeNumber.integerValue;
PHAssetCollectionType type = [self __assetCollectionTypeOfSubtype:subtype];
PHFetchResult *fetchResult =
[PHAssetCollection fetchAssetCollectionsWithType:type
subtype:subtype
options:self.assetCollectionFetchOptions];
[fetchResults addObject:fetchResult];
}
self.fetchResults = fetchResults;
if (fetchResults.count > 0) {
[self getAlbumsCompletion:YES];
}else{
[self getAlbumsCompletion:NO];
}
}
欢迎使用DLPhotoPicker开源库,封装了AssetsLibrary和Photos的API,支持iOS7和iOS8以上
源码下载:https://github.com/darling0825/DLPhotoPicker.git