关于在图库中图片进行多选
来源:互联网 发布:动物建模软件 编辑:程序博客网 时间: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];
}
下面是图片
- 关于在图库中图片进行多选
- 主动通知Android系统图库进行更新(图片保存到手机中,立即在图库中显示1)
- android 在图库中进行幻灯片播放时图片不会全屏显示
- 在Android系统的"图库"中点击某张图片进行分享,在自己的应用程序中如何获取那张图片的路径?
- 【Android】 保存图片到系统图库, 并立即显示在图库中
- android网络下载图片并且显示在图库中
- 拍照后无法在系统图库中找到该图片
- Android 使保存的图片在图库中显示
- Android 使保存的图片在图库中显示
- 在Android手机图库中预先内置图片
- 储存图片刷新图库,使图片在图库第一个
- 将网络图片保存在本地,并且出现在图库中
- 在图库Gallery3D中旋转图片,然后用蓝牙分享失败
- 【android】解决在图库中缩略图与实际图片不对应的问题
- Android-从图库中加载图片到屏幕并在上面画图
- android中从图库中选取图片
- android 从系统图库中选择图片
- Android 从系统图库中选择图片
- MySQL5.7.*免安装版配置方法
- centos7中安装elasticsearch-head、git、nodejs
- Java TCP/UDP socket 编程流程总结
- 【Java NIO的深入研究6】JAVA NIO之Scatter-Gather
- LeetCode 题目:312. Burst Balloons
- 关于在图库中图片进行多选
- 《REWORK》摘录及感想----理想世界和现实世界真的无法平衡吗?
- 专治win各种110120140.dll丢失
- eclipse 安装 scala
- 数据结构实验:连通分量个数——并查集
- 策略模式
- 网络攻防之——nmap工具的使用
- Intent详解
- CCF201509-2-日期计算