ios 相冊 同時選擇多張圖片


源碼: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM