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

来源:互联网 发布:动物建模软件 编辑:程序博客网 时间:2024/06/05 04:38

废话少说,先上效果图




上代码

里面有自带控制张数的

这是被调动页面

#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 = [superinit];

    if (self) {

        allLibrary = [[ALAssetsLibraryalloc]init];

        array = [[NSMutableArrayalloc]init];

        selectedImages = [[NSMutableArrayalloc] init];

        thumbArray = [[NSMutableArrayalloc]init];

        [selfgetAllPicturesFromAlbum];

        

    }

    returnself;

}


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

-(void)getAllPicturesFromAlbum;

{

    //间隔

    float image_Space =10;

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

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

    

    _bgScrollview.backgroundColor = [UIColorgreenColor];

    

    [selfaddSubview:_bgScrollview];

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

    [allLibraryenumerateGroupsWithTypes:ALAssetsGroupAllusingBlock:^(ALAssetsGroup *group,BOOL *stop) {

        //如果每个组存在的话

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

            

            staticint 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 = [[UIImageViewalloc]initWithFrame:CGRectMake(image_x, image_y, image_Size, image_Size)];

                //获得图片

                imageView.backgroundColor = [UIColorredColor];

                imageView.image = [UIImageimageWithCGImage:[resultthumbnail]];

                [_bgScrollviewaddSubview:imageView];


                

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

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

                select.image = [UIImageimageNamed:@"4"];

                [imageView addSubview:select];

                [thumbArrayaddObject:select];

                

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

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

                button.tag = i;

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

                [_bgScrollviewaddSubview:button];

                

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

                

                [selectedImagesaddObject:@"0"];

                

                

                //存进数组

                [arrayaddObject: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;

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

    staticint a = 0;

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

    NSString *selectState = [selectedImagesobjectAtIndex:button.tag];

     UIImageView *imageView = [thumbArrayobjectAtIndex:button.tag];

    if ([selectStateisEqualToString:@"0"]) {

        //如果0未选中

        imageName = @"4";

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

        [selectedImagesreplaceObjectAtIndex:button.tagwithObject:@"1"];

        a++;

    }else{

        //如果1就选中

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

        imageView.image = [UIImageimageNamed:@"4"];

        [selectedImagesreplaceObjectAtIndex:button.tagwithObject:@"0"];

        a--;

    }


    if (a >4) {

        a--;

        imageView.image = [UIImageimageNamed:imageName];

        [selectedImagesreplaceObjectAtIndex:button.tagwithObject:selectState];

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

        UILabel *label = [[UILabelalloc]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 = [UIColorredColor];

        label.textAlignment =NSTextAlignmentCenter;

        [selfaddSubview:label];

        [UIViewanimateWithDuration:2animations:^{

            label.alpha =1;

           

        } completion:^(BOOL finished) {

             label.alpha =0;

        }];

       

    }

}


这是controller里面的代码


#import "ViewController.h"

#import "MoreImageSelectView.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

    MoreImageSelectView *view = [[MoreImageSelectViewalloc]initMoreImageSelectView];

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

    [self.viewaddSubview:view];

}


下面是图片





0 0
原创粉丝点击