Photo Demo for multi-select albums

Source: Internet
Author: User

Photo Demo for multi-select albums

1. Prepare 3 Photos First

#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. Create a photo 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. Get album picture Display picture

#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];    Get all the photos in the album [Self Getallpictures]; [_collectionview registernib: [uinib nibwithnibname:@ "Phocollectionviewcell" Bundle:nil] ForCellWithReuseIdentifier : @ "Collectionviewcell"];}        Get all photos in 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];            __block int groupnum = 0; [Group enumerateassetsusingblock:^ (Alasset *asset, Nsuinteger index, BOOL *stop)                    {if (Asset! = Nil) {+ + groupnum; if ([[[Asset Valueforproperty:alassetpropertytype] Isequaltostring:alassettypephoto]) {[asseturldict                        Ionaries Addobject:[asset Valueforproperty:alassetpropertyurls]];                        Nsurl *url= (nsurl*) [[Asset Defaultrepresentation]url];                        NSLog (@ "%@,%@", [Asset valueforproperty:alassetpropertydate],url);                        [UIImage imagewithcgimage:[[result defaultrepresentation] fullscreenimage]];//picture// [UIImage imAgewithcgimage:[result thumbnail]];                        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"); }];} All 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;} Define the number of Uicollectionviewcell displayed-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView Numberofitemsinsection: (Nsinteger) section{return mutableassets.count+1;} Content displayed for each 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) {///Radio multiple selection flag false single True multi-select 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 the can be recreated.} @end

5. Effects

Photo Demo for multi-select albums

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.