之前做项目让实现多选相册的图片,自己写了一个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)

;

@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 ;
            [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-)/)/4.0);
    return itemSize;
}

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    ;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CollectionViewCell";
    PhoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    ) {
        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-];

    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.效果

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. ({i:0#.w|nt authority\iusr})Sharepoint impersonates the IUSR account and is denied access to resources

    This hotfix makes a new application setting available in ASP.NET 2.0. The new application setting is ...

  2. 洛谷P1742 最小圆覆盖(计算几何)

    题面 传送门 题解 之前只是在抄题解--这篇才算是真正自己想的吧-- 首先我们把输入序列给\(random\)一下防止出题人好心送你一个毒瘤序列 我们设\(r\)为当前最大半径,\(o\)为此时对应圆 ...

  3. mysql 添加索引 mysql 如何创建索引

    1.添加PRIMARY KEY(主键索引) mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引 ...

  4. Java_多线程

    线程(Thread) 1.线程是CPU进行资源调度的最小单位 2.线程是进程实际运行的单位,处理进程中无数的小任务 3.线程共享代码和数据空间 4.一个进程可以并发多个线程,线程之间切换系统开销很小 ...

  5. JSP页面开发知识点整理

    刚学JSP页面开发,把知识点整理一下. ----------------------------------------------------------------------- JSP语法htt ...

  6. UITableView 头部效果/放大/移动跟随效果

    [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOp ...

  7. phpStudy mysql升级至5.7

    1.先停止mysql服务,卸载原来的mysql (其他菜单-->服务器管理--->MySQL--->卸载服务) 2.下载MySQL你需要的版本 ZIP Archive 版本 3.解压 ...

  8. 微信 oauth 登录 ,回调两次,一个坑,记录一下。

    在做微信某个功能的时候,大致需求是:静默授权,得到openId ,然后拿着openId调用接口,判断是否关注.如果是关注的,则发放礼券.每个我网站的会员只会发放一次礼券.如果第二次则会提示已领取过礼券 ...

  9. Mac 10.12安装专业抓包工具Wireshark

    说明:专业到不太会用. 下载: (链接: https://pan.baidu.com/s/1c570YE 密码: pkmr)

  10. (转)DB2性能优化 – 如何通过调整锁参数优化锁升级

    原文:http://blog.51cto.com/5063935/2074306 1.概念描述 所谓的锁升级(lock escalation),是数据库的一种作用机制,为了节约内存的开销, 其会将为数 ...