zoukankan      html  css  js  c++  java
  • 实现多选相册的图片 demo

     
    实现多选相册的图片 demo
     
    1.首先准备3个图片
     
    #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.效果
     
  • 相关阅读:
    <QT学习>串口QSerialPort类同步与异步接收和发送数据
    <QT学习>QT生成与调用共享库
    《Pinctrl和GPIO子系统的使用》
    《查询方式的按键驱动程序》
    《C库 — sprintf字符串格式化命令,把格式化的数据写入某个字符串中》
    《C库 — 字符串合并函数strcat 字符串常量导致程序崩溃》
    Ex 5_22 在此我们基于以下性质给出一个新的最小生成树算法..._第九次作业
    Ex 5_21 无向图G=(V,E)的反馈边集..._第九次作业
    Ex 5_33 实现一个关于公式长度(其中所有文字总的出现次数)为线性时间的Horn公式可满足性问题_第十次作业
    Ex 5_28 Alice想要举办一个舞会..._第十次作业
  • 原文地址:https://www.cnblogs.com/wangxiaorui/p/5168137.html
Copyright © 2011-2022 走看看