Before doing the project to achieve multi-select photo album, I wrote a demo has been saved on the computer, this afternoon found that the computer 128G capacity is fast, ready to clean the computer, so put some of the previous demo put on the blog, later convenient to use.
1. Prepare 3 Photos First
2. Define Cell Phocollectionviewcell
#import <UIKit/UIKit.h>void(^selectbtnclickblock) (BOOL isselect); @interface Phocollectionviewcell:uicollectionviewcell@property (Weak, nonatomic) Iboutlet * ImageView; @property (weak, nonatomic) iboutlet * Selectimageview; @property (nonatomic,copy) selectbtnclickblock Selectbtnclickblock; -(Ibaction) Selectbtnclick: (ID*selectbtn; @end
#import "PhoCollectionViewCell.h"@implementationPhocollectionviewcell- (void) awakefromnib {//Initialization Code }-(Ibaction) Selectbtnclick: (ID) Sender {UIButton*btn= (UIButton *) sender; Btn.selected=!btn.selected; NSLog (@"%@",@"AAAA"); _selectbtnclickblock (btn.selected);}@end
3. Create a photo model
#import <Foundation/Foundation.h>#import <AssetsLibrary/ALAssetsLibrary.h>@ Interface*asset; @property (nonatomic,assign) BOOL isSelected; @end
#import " PhoModel.h " @implementation Phomodel @end
4. Get album picture Display picture
#import "ViewController.h"#import<AssetsLibrary/AssetsLibrary.h>#import "AppDelegate.h"#import "PhoModel.h"#import "PhoCollectionViewCell.h"#defineApplicationdelegate ((Appdelegate *) [uiapplication sharedapplication].delegate)StaticNsinteger count =0;@interfaceViewcontroller () {Nsmutablearray*Mutableassets;} @property (Weak, nonatomic) Iboutlet Uicollectionview*CollectionView;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //get all the photos in an album[self getallpictures]; [_collectionview registernib: [uinib nibwithnibname:@"Phocollectionviewcell"Bundle:nil] Forcellwithreuseidentifier:@"Collectionviewcell"];}//get all the photos in an album-(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]; __blockintGroupnum =0; [Group Enumerateassetsusingblock:^ (Alasset *asset, Nsuinteger index, BOOL *stop) { if(Asset! =Nil) { ++groupnum; if([[[Asset Valueforproperty:alassetpropertytype] Isequaltostring:alassettypephoto]) {[Asseturldi Ctionaries Addobject:[asset Valueforproperty:alassetpropertyurls]]; Nsurl*url= (nsurl*) [[Asset Defaultrepresentation]url]; NSLog (@"%@,%@", [asset valueforproperty:alassetpropertydate],url); //[UIImage Imagewithcgimage:[[result defaultrepresentation] fullscreenimage];//Image//[UIImage imagewithcgimage:[result thumbnail]; //thumbnail ImagesPhomodel*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"); }];}//All Asset-(void) allphotoscollected: (Nsmutablearray *) mutableasset{[Self.collectionview reloaddata];}#pragmaMark-uicollectionviewdatasource-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: ( uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath {cgsize itemsize= Cgsizemake ([UIScreen mainscreen].bounds.size.width- the)/4.0, ([UIScreen mainscreen].bounds.size.width- -)/4.0); returnitemsize;}//defines the number of Uicollectionviewcell to show-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{returnmutableassets.count+1;}//what each Uicollectionview shows-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{StaticNSString * 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"); }; returncell; } 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) {//single-choice multiple-selection mark false Radio True multi-selectBOOL issangal=false; if(issangal) { for(Phomodel *tmpphotomodelinchmutableassets) {tmpphotomodel.isselected=NO; }} phomodel.isselected=Isselect; [_collectionview Reloaddata]; }; returncell;}- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) Indexpath {NSLog (@"%ld", Indexpath.row);}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
5. Effects
iOS Multi-menu select album Pictures