关于在图库中图片进行多选

废话少说,先上效果图




上代码

里面有自带控制张数的

这是被调动页面

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width

#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

#import "MoreImageSelectView.h"

#import <AssetsLibrary/AssetsLibrary.h>

@implementation MoreImageSelectView

{

    //所有图片库的对象

    ALAssetsLibrary *allLibrary;

    UIScrollView *_bgScrollview;

    NSMutableArray *selectedImages;

    NSMutableArray *thumbArray;

    NSMutableArray *array;

}

-(instancetype)initMoreImageSelectView

{

    self = [super init];

    if (self) {

        allLibrary = [[ALAssetsLibrary alloc]init];

        array = [[NSMutableArray alloc]init];

        selectedImages = [[NSMutableArray alloc] init];

        thumbArray = [[NSMutableArray alloc]init];

        [self getAllPicturesFromAlbum];

        

    }

    return self;

}


#pragma mark --获得图库中的所有图片--

-(void)getAllPicturesFromAlbum;

{

    //间隔

    float image_Space = 10;

    float image_Size = (SCREENWIDTH - image_Space * 4)/3.0;

    _bgScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];

    

    _bgScrollview.backgroundColor = [UIColor greenColor];

    

    [self addSubview:_bgScrollview];

    NSLog(@"--------%ld",array.count);

    [allLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

        //如果每个组存在的话

        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

            

            static int i = 0;

            if (result) {

                //图片的x

                float image_x = i%3 * (image_Size + image_Space) + image_Space;

                //图片的y

                float image_y = i/3 * (image_Size + image_Space) + image_Space;

                

                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(image_x, image_y, image_Size, image_Size)];

                //获得图片

                imageView.backgroundColor = [UIColor redColor];

                imageView.image = [UIImage imageWithCGImage:[result thumbnail]];

                [_bgScrollview addSubview:imageView];


                

                //在每一张图片上添加一个复选框

                UIImageView *select = [[UIImageView alloc]initWithFrame:CGRectMake(image_Size - 20, 0, 20, 20)];

                select.image = [UIImage imageNamed:@"4"];

                [imageView addSubview:select];

                [thumbArray addObject:select];

                

                //每张图片添加一个响应按钮

                UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(image_x, image_y, image_Size, image_Size)];

                button.tag = i;

                [button addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];

                [_bgScrollview addSubview:button];

                

                //初始化已经选择的图片的数组

                

                [selectedImages addObject:@"0"];

                

                

                //存进数组

                [array addObject:result];

            }

            i++;

            NSInteger a = i/3;

            NSInteger b = i%3;

            if (b > 0) {

                a = a + 1;

            }

             _bgScrollview.contentSize = CGSizeMake(self.frame.size.width, image_Space + (image_Space + image_Size) * a);

        }];

        

    } failureBlock:^(NSError *error) {

        

    }];

}


#pragma mark --点击图片时的响应按钮--

-(void)imageButtonAction:(UIButton *)button

{

    //记录图片的名称

    NSString *imageName;

    //为了最多选取六张才加的条件

    static int a = 0;

    //当点击某个图片的时候先判断该图片是否已被选中

    NSString *selectState = [selectedImages objectAtIndex:button.tag];

     UIImageView *imageView = [thumbArray objectAtIndex:button.tag];

    if ([selectState isEqualToString:@"0"]) {

        //如果0未选中

        imageName = @"4";

        imageView.image = [UIImage imageNamed:@"4-拷贝-2"];

        [selectedImages replaceObjectAtIndex:button.tag withObject:@"1"];

        a++;

    }else{

        //如果1就选中

        imageName = @"4-拷贝-2";

        imageView.image = [UIImage imageNamed:@"4"];

        [selectedImages replaceObjectAtIndex:button.tag withObject:@"0"];

        a--;

    }


    if (a > 4) {

        a--;

        imageView.image = [UIImage imageNamed:imageName];

        [selectedImages replaceObjectAtIndex:button.tag withObject:selectState];

        //提示用户最多选取六张

        UILabel *label = [[UILabel alloc]init];

        label.center = CGPointMake(SCREENWIDTH/2, SCREENHEIGHT/4*3);

        label.bounds = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT/20);

        label.text = @"最多选择6张图片";

        label.alpha = 0;

        label.textColor = [UIColor redColor];

        label.textAlignment = NSTextAlignmentCenter;

        [self addSubview:label];

        [UIView animateWithDuration:2 animations:^{

            label.alpha = 1;

           

        } completion:^(BOOL finished) {

             label.alpha = 0;

        }];

       

    }

}


这是controller里面的代码


#import "ViewController.h"

#import "MoreImageSelectView.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    MoreImageSelectView *view = [[MoreImageSelectView alloc]initMoreImageSelectView];

    view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);

    [self.view addSubview:view];

}


下面是图片





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值