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

1.首先准备3个图片

2.定义单元格PhoCollectionViewCell

  1. #import <UIKit/UIKit.h>
  2.  
  3. typedef void(^SelectBtnClickBlock) (BOOL isSelect);
  4.  
  5. @interface PhoCollectionViewCell : UICollectionViewCell
  6.  
  7. @property (weak ,nonatomic) IBOutlet UIImageView * imageView;
  8.  
  9. @property (weak ,nonatomic) IBOutlet UIImageView * selectImageView;
  10.  
  11. @property (nonatomic,copy) SelectBtnClickBlock selectBtnClickBlock;
  12.  
  13. - (IBAction)selectBtnClick:(id)sender;
  14.  
  15. @property (weak, nonatomic) IBOutlet UIButton *selectBtn;
  16.  
  17. @end
  1. #import "PhoCollectionViewCell.h"
  2.  
  3. @implementation PhoCollectionViewCell
  4.  
  5. - (void)awakeFromNib {
  6. // Initialization code
  7.  
  8. }
  9.  
  10. - (IBAction)selectBtnClick:(id)sender {
  11. UIButton *btn=(UIButton *)sender;
  12. btn.selected=!btn.selected;
  13. NSLog(@"%@",@"aaaa");
  14. _selectBtnClickBlock(btn.selected);
  15. }
  16. @end

3.创建相片Model

  1. #import <Foundation/Foundation.h>
  2. #import <AssetsLibrary/ALAssetsLibrary.h>
  3.  
  4. @interface PhoModel : NSObject
  5.  
  6. @property(nonatomic,strong) ALAsset *asset;
  7. @property (nonatomic,assign) BOOL isSelected;
  8. @end
  1. #import "PhoModel.h"
  2.  
  3. @implementation PhoModel
  4.  
  5. @end

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

  1. #import "ViewController.h"
  2. #import <AssetsLibrary/AssetsLibrary.h>
  3.  
  4. #import "AppDelegate.h"
  5. #import "PhoModel.h"
  6. #import "PhoCollectionViewCell.h"
  7.  
  8. #define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
  9.  
  10. ;
  11.  
  12. @interface ViewController ()
  13. {
  14. NSMutableArray *mutableAssets;
  15. }
  16. @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
  17.  
  18. @end
  19.  
  20. @implementation ViewController
  21.  
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24.  
  25. //获取相册中的全部照片
  26. [self getAllPictures];
  27. [_collectionView registerNib: [UINib nibWithNibName:@"PhoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
  28. }
  29.  
  30. //获取相册中的全部照片
  31. -(void)getAllPictures {
  32. mutableAssets = [[NSMutableArray alloc]init];
  33.  
  34. NSMutableArray *assetURLDictionaries = [[NSMutableArray alloc] init];
  35. NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
  36.  
  37. __block NSMutableArray *tempMutableAssets = mutableAssets;
  38. __block ViewController *tempSelf = self;
  39. __block NSMutableArray *tempAssetGroups = assetGroups;
  40.  
  41. [ApplicationDelegate.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop){
  42. if (group != nil) {
  43. count = [group numberOfAssets];
  44. __block ;
  45. [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
  46. if(asset != nil) {
  47. ++ groupNum;
  48. if([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
  49. [assetURLDictionaries addObject:[asset valueForProperty:ALAssetPropertyURLs]];
  50. NSURL *url= (NSURL*) [[asset defaultRepresentation]url];
  51. NSLog(@"%@,%@",[asset valueForProperty:ALAssetPropertyDate],url);
  52.  
  53. // [UIImage imageWithCGImage:[[result defaultRepresentation] fullScreenImage]];//图片
  54. // [UIImage imageWithCGImage:[result thumbnail]]; //缩略图
  55.  
  56. PhoModel *phoModel=[[PhoModel alloc]init];
  57. phoModel.asset=asset;
  58. phoModel.isSelected=NO;
  59. [tempMutableAssets addObject:phoModel];
  60. if (tempMutableAssets.count == groupNum) {
  61. [tempSelf allPhotosCollected:tempMutableAssets];
  62. }
  63. }
  64. }
  65. }];
  66. [tempAssetGroups addObject:group];
  67. }
  68. }failureBlock:^(NSError *error){
  69. NSLog(@"There is an error");
  70. }];
  71. }
  72.  
  73. //所有asset
  74. -(void)allPhotosCollected:(NSMutableArray *)mutableAsset{
  75. [self.collectionView reloadData];
  76. }
  77.  
  78. #pragma mark -- UICollectionViewDataSource
  79.  
  80. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  81. CGSize itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width-)/)/4.0);
  82. return itemSize;
  83. }
  84.  
  85. //定义展示的UICollectionViewCell的个数
  86. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  87. {
  88. ;
  89. }
  90. //每个UICollectionView展示的内容
  91. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. static NSString * CellIdentifier = @"CollectionViewCell";
  94. PhoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
  95. ) {
  96. cell.imageView.image = [UIImage imageNamed:@"0.png"];
  97. cell.selectImageView.hidden=YES;
  98. cell.selectBtnClickBlock=^(BOOL isSelect)
  99. {
  100. NSLog(@"cell1 block");
  101. };
  102. return cell;
  103. }
  104.  
  105. PhoModel *phoModel = mutableAssets[indexPath.row-];
  106.  
  107. cell.imageView.image = [UIImage imageWithCGImage:[phoModel.asset thumbnail]];
  108.  
  109. if (phoModel.isSelected) {
  110. cell.selectImageView.image=[UIImage imageNamed:@"2.png"];
  111. }
  112. else
  113. {
  114. cell.selectImageView.image=[UIImage imageNamed:@"1.png"];
  115. }
  116. cell.selectImageView.hidden=NO;
  117. cell.selectBtn.selected=phoModel.isSelected;
  118. cell.selectBtnClickBlock=^(BOOL isSelect)
  119. {
  120. //单选多选标记 false 单选 true 多选
  121. BOOL issangal=false;
  122. if (issangal) {
  123. for (PhoModel *tmpPhotoModel in mutableAssets) {
  124. tmpPhotoModel.isSelected = NO;
  125. }
  126. }
  127. phoModel.isSelected=isSelect;
  128. [_collectionView reloadData];
  129. };
  130.  
  131. return cell;
  132. }
  133. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  134. NSLog(@"%ld",indexPath.row);
  135. }
  136.  
  137. - (void)didReceiveMemoryWarning {
  138. [super didReceiveMemoryWarning];
  139. // Dispose of any resources that can be recreated.
  140. }
  141.  
  142. @end

5.效果

IOS多选单选相册图片的更多相关文章

  1. ios获取摄像头与相册图片

    iOS的一些设备上都安装了摄像头.现在绝大多数都有了. 在编程中,我们是用相应的东西来进行照相,录像等功能.   一.UIImagePickerController类 UIImagePickerCon ...

  2. iOS QRcode识别及相册图片二维码读取识别

    https://www.jianshu.com/p/48e44fe67c1d 2016.03.30 10:32* 字数 892 阅读 16197评论 5喜欢 34赞赏 1 最近碰到一个用户 在使用我们 ...

  3. iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK

    iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言:    今天给大家 ...

  4. Android 仿微信朋友圈发动态功能(相册图片多选)

    代码分享 代码名称: 仿微信朋友圈发动态功能(相册图片多选) 代码描述: 仿微信朋友圈发动态功能(相册图片多选) 代码托管地址: http://www.apkbus.com/android-15276 ...

  5. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  6. 使用JS调用手机本地摄像头或者相册图片识别二维码/条形码

    接着昨天的需求,不过这次不依赖微信,使用纯js唤醒手机本地摄像头或者选择手机相册图片,识别其中的二维码或者是条形码.昨天,我使用微信扫一扫识别,效果超棒的.不过如果依赖微信的话,又怎么实现呢,这里介绍 ...

  7. ios 上传视频或图片

    关于iOS如何实现视频和图片的上传, 我们先理清下思路 思路: #1. 如何获取图片? #2. 如何获取视频? #3. 如何把图片存到缓存路径中? #4. 如何把视频存到缓存路径中? #5. 如何上传 ...

  8. iOS关于UILabel 基本属性 背景图片 背景色

    [代码] iOS关于UILabel 基本属性 背景图片 背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  9. android开发之——获取相册图片和路径

    Android开发获取相册图片的方式网上有很多种,这里说一个Android4.4后的方法,因为版本越高,一些老的api就会被弃用,新的api和老的api不兼容,导致出现很多问题. 比如:managed ...

随机推荐

  1. (zxing.net)一维码Code 128的简介、实现与解码

    一.简介 一维码Code 128:1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,相对较为复杂,支持的字元也相对较多,又有不同的编码方式可供交互运用,因此其应用弹性也较大 ...

  2. .net core 应用Nancy快速实现轻量级webapi

    目前大量数据接口均采用API的方式为各类应用提供数据服务.Nancy是.net下实现webapi的一个轻量级的框架,可以快速搭建一个api服务环境,是一种快速建立api服务的不错选择. 本文记录.ne ...

  3. c#去除DataTable空列

    网上搜了好多,没找到能用的,自己写一个,有发现错误的给我留言. private void RemoveNULLColumns(ref DataTable data)//删除空列 { try { Dat ...

  4. 当我们在谈论multidex65535时,我们在谈论什么

    本文来自网易云社区 作者:郑文 首先我们并不在讨论车牌号.本文尽量避免谈论重复的技术点,只探讨一下multidex提供给我们的技术启示. 原理 multidex技术原理可以分成两个部分: 在app启动 ...

  5. linux网络流量实时监控工具之iptraf 【个人比较喜欢用的流量监控软件】

    linux网络流量实时监控工具之iptraf IPTraf是一个网络监控工具,功能比nload更强大,可以监控所有的流量,IP流量,按协议分的流量,还可以设置过滤器等,如下图 对监控网络来说,这个更适 ...

  6. PAT乙级1091-1095

    1091 N-自守数 (15 分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守数 ...

  7. php扩展编译流程路

  8. 第三天,爬取伯乐在线文章代码,编写items.py,保存数据到本地json文件中

        一. 爬取http://blog.jobbole.com/all-posts/中的所有文章     1. 编写jobbole.py简单代码 import scrapy from scrapy. ...

  9. Ionic2使用TypeScript调用自定义JavaScript脚本

    在项目app目录下面写一个.d.ts 里面声明你要引用JS库里面定义的变量,变量名要保持一致 declare var Strophe: any; 然后把JS库放在www目录下面 然后在index.ht ...

  10. P1975 [国家集训队]排队

    题目链接 题意分析 我们考虑 交换两个数\([le,ri]\)的贡献 减少的逆序对数\([le,ri]\)中小于\(num[le]\)以及大于\(num[ri]\)的数 增加的\([le,ri]\)中 ...