本文實例為大家分享了iOS實現相冊多選圖片上傳的具體代碼,供大家參考,具體內容如下
原理:獲取手機里的全部照片,顯示在自定義的視圖里
//獲取到相冊的所有圖片- (void)addAllPhotos{ @WeakObj(self); _assetsLibrary=[[ALAssetsLibrary alloc]init]; [_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { @StrongObj(self); if (group) { [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (result) { // 把相冊儲存到數組中,方便后面展示相冊時使用 CGImageRef cgImage = [result thumbnail]; UIImage *image = [UIImage imageWithCGImage:cgImage]; NSData *imageData = UIImageJPEGRepresentation(image,0.5); UIImage *newImage = [UIImage imageWithData:imageData]; UZGPhotoChooseModel *model=[[UZGPhotoChooseModel alloc]init]; model.image=newImage; model.currentImageIsSelect=NO;//初始化默認未選中 model.showBackView=NO;//是否顯示背景遮罩 model.row=index; [self.cellInfoArray addObject:model]; [self.myCollectionView reloadData]; } }]; } } failureBlock:^(NSError *error) { }];}//選中圖片后刷新圖片上面的選中數字- (selectCellBolck)reloadCellSelectBlock:(NSIndexPath *)indexPath{ @WeakObj(self); __block UZGPhotoChooseModel *model=self.cellInfoArray[indexPath.row]; selectCellBolck block=^(BOOL current_isSelect){ if (selfWeak.selectNum>self.max_selectNum) { return; } if (current_isSelect) { selfWeak.selectNum++; }else{ selfWeak.selectNum--; } model.currentImageIsSelect=current_isSelect; model.totalNum=selfWeak.selectNum; [selfWeak.cellInfoArray replaceObjectAtIndex:indexPath.row withObject:model]; [selfWeak reloadCurrentNum]; [selfWeak.myCollectionView reloadData]; }; return block;}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答