IOS多选单选相册图片

news/2023/12/10 14:52:57

之前做项目让实现多选相册的图片,自己写了一个demo一直保存在电脑上,今天下午发现电脑128G的容量已经快没有了,准备清理电脑,所以把之前做的一些demo放在博客上,以后方便用。

1.首先准备3个图片

2.定义单元格PhoCollectionViewCell

#import <UIKit/UIKit.h>typedef void(^SelectBtnClickBlock) (BOOL isSelect);@interface PhoCollectionViewCell : UICollectionViewCell@property (weak ,nonatomic)  IBOutlet  UIImageView *  imageView;@property (weak ,nonatomic)  IBOutlet  UIImageView *  selectImageView;@property (nonatomic,copy) SelectBtnClickBlock selectBtnClickBlock;- (IBAction)selectBtnClick:(id)sender;@property (weak, nonatomic) IBOutlet UIButton *selectBtn;@end
#import "PhoCollectionViewCell.h"@implementation PhoCollectionViewCell- (void)awakeFromNib {// Initialization code
    
}- (IBAction)selectBtnClick:(id)sender {UIButton *btn=(UIButton *)sender;btn.selected=!btn.selected;NSLog(@"%@",@"aaaa");_selectBtnClickBlock(btn.selected);
}
@end

3.创建相片Model

#import <Foundation/Foundation.h>
#import <AssetsLibrary/ALAssetsLibrary.h>@interface PhoModel : NSObject@property(nonatomic,strong) ALAsset *asset;
@property (nonatomic,assign) BOOL isSelected;
@end
#import "PhoModel.h"@implementation PhoModel@end

 

4.获取相册图片显示图片

#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>#import "AppDelegate.h"
#import "PhoModel.h"
#import "PhoCollectionViewCell.h"#define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)static NSInteger count = 0;@interface ViewController ()
{NSMutableArray *mutableAssets;
}
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//获取相册中的全部照片
    [self getAllPictures];[_collectionView registerNib: [UINib nibWithNibName:@"PhoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
}//获取相册中的全部照片
-(void)getAllPictures {mutableAssets = [[NSMutableArray alloc]init];NSMutableArray *assetURLDictionaries = [[NSMutableArray alloc] init];NSMutableArray *assetGroups = [[NSMutableArray alloc] init];__block NSMutableArray *tempMutableAssets = mutableAssets;__block ViewController *tempSelf = self;__block NSMutableArray *tempAssetGroups = assetGroups;[ApplicationDelegate.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){if (group != nil) {count = [group numberOfAssets];__block int groupNum = 0;[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){if(asset != nil) {++ groupNum;if([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {[assetURLDictionaries addObject:[asset valueForProperty:ALAssetPropertyURLs]];NSURL *url= (NSURL*) [[asset defaultRepresentation]url];NSLog(@"%@,%@",[asset valueForProperty:ALAssetPropertyDate],url);//                        [UIImage imageWithCGImage:[[result defaultRepresentation] fullScreenImage]];//图片
//                        [UIImage imageWithCGImage:[result thumbnail]];    //缩略图
                        PhoModel *phoModel=[[PhoModel alloc]init];phoModel.asset=asset;phoModel.isSelected=NO;[tempMutableAssets addObject:phoModel];if (tempMutableAssets.count == groupNum) {[tempSelf allPhotosCollected:tempMutableAssets];}}}}];[tempAssetGroups addObject:group];}}failureBlock:^(NSError *error){NSLog(@"There is an error");}];
}//所有asset
-(void)allPhotosCollected:(NSMutableArray *)mutableAsset{[self.collectionView reloadData];
}#pragma mark -- UICollectionViewDataSource- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {CGSize itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-15)/4.0, ([UIScreen mainScreen].bounds.size.width-30)/4.0);return itemSize;
}//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{return mutableAssets.count+1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{static NSString * CellIdentifier = @"CollectionViewCell";PhoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];if (indexPath.row==0) {cell.imageView.image = [UIImage imageNamed:@"0.png"];cell.selectImageView.hidden=YES;cell.selectBtnClickBlock=^(BOOL isSelect){NSLog(@"cell1 block");};return cell;}PhoModel *phoModel = mutableAssets[indexPath.row-1];cell.imageView.image = [UIImage imageWithCGImage:[phoModel.asset thumbnail]];if (phoModel.isSelected) {cell.selectImageView.image=[UIImage imageNamed:@"2.png"];}else{cell.selectImageView.image=[UIImage imageNamed:@"1.png"];}cell.selectImageView.hidden=NO;cell.selectBtn.selected=phoModel.isSelected;cell.selectBtnClickBlock=^(BOOL isSelect){//单选多选标记 false 单选 true 多选BOOL issangal=false;if (issangal) {for (PhoModel *tmpPhotoModel in mutableAssets) {tmpPhotoModel.isSelected = NO;}}phoModel.isSelected=isSelect;[_collectionView reloadData];};return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {NSLog(@"%ld",indexPath.row);
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

5.效果

 

转载于:https://www.cnblogs.com/5ishare/p/4832242.html


https://dhexx.cn/news/show-16929.html

相关文章

onkeydown为什么会无限回调_基金操作:为什么一个小回调都能把你们吓退?

今天私信老A最多的一个词语就是问&#xff0c;今天可以加仓吗&#xff1f;加多少&#xff1f;证明一个问题&#xff0c;韭菜开始不怕了&#xff0c;这个是信号是非常可怕的&#xff01;一、散户不怕的市场风险会比较高的。1、证明散户认同了这次反弹确认了&#xff0c;开始想进…

adminlte3 动态菜单_如何优雅的在线制作一个动态表情?

请问如何优雅的在线制作一个动态表情&#xff1f;就是……来SOOGIF&#xff01;用电脑打开SOOGIF的网站&#xff1a;www.soogif.com&#xff0c;点击“工具制作"菜单栏&#xff0c;就可以开始相应的操作啦&#xff0c;手机端暂时不支持哦。▲打开官网&#xff0c;点击“工…

java中的依赖与继承的关系_面向对象类关系(继承、实现、依赖、关联、聚合、组合)...

在进行一个OO系统设计时候我们需要根据系统的需求来抽象出一些类&#xff0c;并且设计类与类之间的关系&#xff0c;也就是我们常说的业务建模&#xff0c;设计优良的类间关系是实现我们常说的“高内聚&#xff0c;低耦合”系统的前提条件&#xff0c;这里我就来梳理下面向对象…

hibernate3 所需的jar包

转载于:https://www.cnblogs.com/zhadafei/p/4191483.html

double小数点后最多几位_减肥有多重要?看看几位明星的减肥成功后的样子。

近日&#xff0c;正在横店拍摄《皓衣行》的陈飞宇被爆出一组上班路透照&#xff0c;身穿日常T恤&#xff0c;却顶着古风高马尾&#xff0c;特别有穿越的感觉&#xff0c;露出帅气的五官&#xff0c;非常有墨燃的味道了~要说在演戏这方面&#xff0c;00后陈飞宇年纪不大&#xf…

db2的离线备份和还原

db2cmd中运行命令 1.做一个离线备份&#xff08;db2cmd&#xff09;mstsc—>db2cmd db2 connect to mydb #连接数据库 db2 list applications &#xff03;显示数据库应用状态 db2 force applications all &…

微信翻译生日快乐的代码_微信表情翻译表白代码彩蛋大全 微信表情加符号翻译意思全解...

微信表情翻译表白代码是多少&#xff1f;相信很多用户都还不太清楚表情翻译表白代码彩蛋&#xff0c;下面让小编为大家带来微信表情加符号翻译意思全解。微信表情翻译表白代码彩蛋大全1、【Emm?】Emm就是上面图片中的表情翻译&#xff1a;你是在说我吗?2、【Emm/】翻译&#…

java 腐蚀 膨胀_Java+opencv3.2.0之膨胀与腐蚀

腐蚀与膨胀是最基本的形态学操作&#xff0c;它们能够实现多种多样的功能&#xff0c;主要如下&#xff1a;1) 消除噪声2) 分割出独立的图像元素&#xff0c;在图像中连接相邻的元素3) 寻找图像中的明显的极大值区域或极小值区域4) 求出图像的梯度膨胀是求局部最大值的操作。本…

iphone微信美颜插件_iOS 微信 7.0.13 内测,Activator 再次更新

本文支持深色预览说重点&#xff1a;iOS 微信 7.0.13 已内测&#xff0c;主要改进小程序屏蔽特殊符号为主&#xff0c;另外&#xff01;Activator 手势已加入A12/13支持。今天不说手机越狱消息&#xff0c;因为作者们还未更新&#xff1a; unc0ver4.4.0版本未推出checkra1n win…

Java新特性--Stream的全部用法整理

文章目录一、流二、常用方法1、filter(element -> boolean表达式)2、distinct()3、sorted() / sorted((T, T) -> int)4、limit(long n)5、skip(long n)6、map(T -> R)7、faltMap(T -> Stream)8、anyMatch(T -> boolean表达式)9、allMatch(T -> boolean)和non…