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
AI 代码解读


#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
AI 代码解读

3.创建相片Model


#import <Foundation/Foundation.h>
#import <AssetsLibrary/ALAssetsLibrary.h>

@interface PhoModel : NSObject

@property(nonatomic,strong) ALAsset *asset;
@property (nonatomic,assign) BOOL isSelected;
@end
AI 代码解读


#import "PhoModel.h"

@implementation PhoModel

@end
AI 代码解读

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
AI 代码解读

5.效果

 

目录
打赏
0
0
0
0
18
分享
相关文章
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
88 1
iOS TextView插入表情或者图片后字体变大或变小
iOS TextView插入表情或者图片后字体变大或变小
137 1
基于iOS平台的高效图片缓存策略实现
【4月更文挑战第22天】 在移动应用开发中,图片资源的加载与缓存是影响用户体验的重要因素之一。尤其对于iOS平台,由于设备存储空间的限制以及用户对流畅性的高要求,设计一种合理的图片缓存策略显得尤为关键。本文将探讨在iOS环境下,如何通过使用先进的图片缓存技术,包括内存缓存、磁盘缓存以及网络请求的优化,来提高应用的性能和响应速度。我们将重点分析多级缓存机制的设计与实现,并对可能出现的问题及其解决方案进行讨论。
实现iOS平台的高效图片缓存策略
【4月更文挑战第22天】在移动应用开发中,图片资源的处理是影响用户体验的重要因素之一。特别是对于图像资源密集型的iOS应用,如何有效地缓存图片以减少内存占用和提升加载速度,是开发者们面临的关键挑战。本文将探讨一种针对iOS平台的图片缓存策略,该策略通过结合内存缓存与磁盘缓存的机制,并采用先进的图片解码和异步加载技术,旨在实现快速加载的同时,保持应用的内存效率。
实现iOS平台的高效图片缓存策略
【4月更文挑战第23天】在移动应用开发领域,尤其是图像处理密集型的iOS应用中,高效的图片缓存策略对于提升用户体验和节省系统资源至关重要。本文将探讨一种针对iOS平台设计的图片缓存方案,该方案通过结合内存缓存与磁盘缓存的多层次结构,旨在优化图片加载性能并降低内存占用。我们将深入分析其设计理念、核心组件以及在实际场景中的应用效果,同时对比其他常见缓存技术的优势与局限。
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
774 1
iOS不支持WebP格式图片解决方案和iPhone 7及其后硬件拍照的HEIC格式图片
iOS 动态权限管理:向用户索取相机和相册访问权限
【4月更文挑战第16天】 在移动应用开发中,尤其是针对iOS平台,用户隐私保护已成为不可忽视的要素。随着苹果对隐私政策的不断收紧,如何优雅地向用户请求访问其设备上敏感资源的权限,成为了开发者必须面对的挑战。本文将深入探讨如何在iOS应用中实现动态权限管理,重点讨论相机和相册访问权限的请求过程,并指导读者通过编程方式提升用户体验与满足数据保护规范之间的平衡。
|
10月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
70 0
实现iOS平台的高效图片缓存策略
【4月更文挑战第4天】在移动应用开发中,图片资源的加载与缓存是影响用户体验的关键因素之一。尤其对于iOS平台,由于设备存储和内存资源的限制,设计一个高效的图片缓存机制尤为重要。本文将深入探讨在iOS环境下,如何通过技术手段实现图片的高效加载与缓存,包括内存缓存、磁盘缓存以及网络层面的优化,旨在为用户提供流畅且稳定的图片浏览体验。

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    31
  • 2
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
    108
  • 3
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    3
  • 4
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    2
  • 5
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    9
  • 6
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    1
  • 7
    iOS各个证书生成细节
    6
  • 8
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    7
  • 9
    【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记5 Data Structures、Methods、Properties
    2
  • 10
    iOS:应用程序的线程安全性
    1
  • AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等