IOS多选单选相册图片

简介:

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

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

 

相关文章
|
27天前
|
JSON JavaScript 安全
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
23 1
|
7月前
|
iOS开发
iOS TextView插入表情或者图片后字体变大或变小
iOS TextView插入表情或者图片后字体变大或变小
67 1
|
8月前
|
Android开发 iOS开发
iOS 替换WebView网页图片为本地图片
iOS 替换WebView网页图片为本地图片
196 0
|
10天前
|
存储 缓存 iOS开发
基于iOS的高效图片缓存策略实现
【4月更文挑战第9天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。特别是对于iOS平台,合理设计图片缓存策略不仅能够提升用户浏览图片时的流畅度,还能有效降低应用程序的内存压力。本文将介绍一种针对iOS环境优化的图片缓存技术,该技术通过多级缓存机制和内存管理策略,实现了图片快速加载与低内存消耗的目标。我们将从系统架构、关键技术细节以及性能评估等方面展开讨论,为开发者提供一套实用的图片缓存解决方案。
15 0
|
15天前
|
存储 缓存 iOS开发
实现iOS平台的高效图片缓存策略
【4月更文挑战第4天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的关键因素之一。尤其对于iOS平台,由于设备存储和内存资源的限制,设计一个高效的图片缓存机制尤为重要。本文将深入探讨在iOS环境下,如何通过技术手段实现图片的高效加载与缓存,包括内存缓存、磁盘缓存以及网络层面的优化,旨在为用户提供流畅且稳定的图片浏览体验。
|
3月前
|
JSON JavaScript 安全
iOS 应用程序数据保护:如何保护 iOS 应用程序中的图片、资源和敏感数据
iOS 应用程序数据保护:如何保护 iOS 应用程序中的图片、资源和敏感数据
|
7月前
|
缓存 iOS开发
iOS LaunchScreen.storyboard 启动页设置图片不显示
iOS LaunchScreen.storyboard 启动页设置图片不显示
135 0
|
8月前
|
iOS开发
iOS Image根据TintColor进行绘制图片(UIImageRenderingMode)
iOS Image根据TintColor进行绘制图片(UIImageRenderingMode)
103 0
|
8月前
|
Swift iOS开发
iOS PhotoBrowser 横竖屏图片浏览器
iOS PhotoBrowser 横竖屏图片浏览器
103 0
|
11月前
|
编解码 iOS开发
iOS 图片链接含有中文图片无法显示问题解决办法
iOS 图片链接含有中文图片无法显示问题解决办法
180 0