源碼:https://github.com/iOSSinger/SGImagePickerController

需要導入這個頭文件
#import <AssetsLibrary/AssetsLibrary.h>
1.獲取相冊分組
- (NSMutableArray *)groups{
if (_groups == nil) {
_groups = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(group){
[_groups addObject:group];
[self.tableView reloadData];
}
} failureBlock:^(NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"訪問相冊失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];
}];
});
}
return _groups;
}
2. 遍歷一組中的資源,包括圖片視頻等,我們只需要圖片
- (void)setGroup:(ALAssetsGroup *)group{
_group = group;
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset == nil) return ;
if (![[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {//不是圖片
return;
}
SGAssetModel *model = [[SGAssetModel alloc] init];
model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
model.imageURL = asset.defaultRepresentation.url;
[self.assetModels addObject:model];
}];
}
3.遍歷可以拿到圖片縮略圖,原圖的URL 圖片拍攝地點 拍攝時間等信息,我們只需要縮略圖用來展示,原圖URL用來獲取原圖
根據URL獲取原圖,系統應該是在子線程中的獲取原圖,注意此處!!!
- (void)originalImage:(void (^)(UIImage *))returnImage{
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = asset.defaultRepresentation;
CGImageRef imageRef = rep.fullResolutionImage;
UIImage *image = [UIImage imageWithCGImage:imageRef scale:rep.scale orientation:(UIImageOrientation)rep.orientation];
if (image) {
returnImage(image);
}
} failureBlock:^(NSError *error) {
}];
}
其他代碼就是處理這些數據,模型,展示效果等,詳細代碼見github,歡迎指正
源碼:https://github.com/iOSSinger/SGImagePickerController
