ios8.0以前获取相册有UIImagePicker(但是每次只能选取一张),如果需要选取多张,只能自己来自定义picker
这里用到的框架为AssetsLibrary.framework,资源库,
#import <AssetsLibrary/ALAsset.h> //图片的基本信息
#import <AssetsLibrary/ALAssetsFilter.h> //Group的过滤器,记住,只能生效一个
#import <AssetsLibrary/ALAssetsGroup.h>//获取相册
#import <AssetsLibrary/ALAssetsLibrary.h>//额 整个类库的管理类吧
#import <AssetsLibrary/ALAssetRepresentation.h> //图片的详细信息
我这边简单的封装了一下,大致的流程就是先获取相册,然后再获取相册中的照片,比较简单,直接上代码吧
- (NSMutableDictionary *)detailGroups { if (!_detailGroups) { _detailGroups = @{}.mutableCopy; } return _detailGroups; } - (ALAssetsLibrary *)library { if (!_library) { _library = [[ALAssetsLibrary alloc]init]; } return _library; } - (NSMutableArray *)assesGroups { if (!_assesGroups) { _assesGroups = @[].mutableCopy; } return _assesGroups; } + (LIUAsset *)shareAsset { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _shareAsset = [[LIUAsset alloc]init]; }); return _shareAsset; } - (void)releaseObj { if (_shareAsset) { _shareAsset = nil; } } ///获取所有的分组 - (void)getAssetsGroups:(GetAssetsGroupsBlock)assetsGroup { if (self.assesGroups.count != 0) { if (assetsGroup) { assetsGroup(self.assesGroups.copy); return; }; } [self.library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { //只需要photo ALAssetsFilter *onlyPhotos = [ALAssetsFilter allPhotos]; if (group) { [group setAssetsFilter:onlyPhotos]; if ([group numberOfAssets] > 0) { [self.assesGroups addObject:group]; } }else { //这里本来应该是要按照*stop来判断的,貌似调试的时候发现这个stop一直是no,但是有一个参考的是这里的group当是最后一个的时候 会是nil,就根据这个判断吧 if (assetsGroup) { assetsGroup(self.assesGroups.copy); return; } } } failureBlock:^(NSError *error) { }]; } ///获取分组所有的alasset - (void)getAllAssetsByGroup:(ALAssetsGroup *)group Finish:(GetAssetsBlock)finish{ NSString *groupName = [group valueForProperty:ALAssetsGroupPropertyName]; __block NSMutableArray *allAssets = [self.detailGroups objectForKey:groupName]; if (allAssets) { //返回数据了 if (finish) { finish(allAssets.copy); } return; } //如果没有数据 allAssets = @[].mutableCopy; [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { //不知道为什么这里的stop显示不对呀 if (result) { [allAssets addObject:result]; }else { //先把数组保存到字典中 [self.detailGroups setObject:allAssets forKey:groupName]; if (finish) { finish(allAssets.copy); } return; } }]; } ///获取全屏图 - (UIImage *)getFullImageWitAsset:(ALAsset *)asset { return [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]; } ///获取缩略图 - (UIImage *)getThumbImageWithAsset:(ALAsset *)asset { return [UIImage imageWithCGImage:[asset thumbnail]]; }
ios 8以后,苹果就舍弃了这个类库,新建了一个photos类,下次有空再整理一下这个类库的
时间: 2024-10-06 02:28:40