iOS选择照片九宫格动态布局

直接上源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// CertificateAddImageView.m
// StuExcGrades
//
// Created by LiuXin on 16/4/19.
// Copyright © 2016年 Adinnet. All rights reserved.
//
#define imageW (([UIScreen mainScreen].bounds.size.width)/3) // 图片宽度高度
#define kMaxColumn 3 // 每行显示数量
#define MaxImageCount 6 // 最多显示图片个数
#define deleImageWH 25 // 删除按钮的宽高

#import "CertificateAddImageView.h"
@interface CertificateAddImageView()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
// 标识被编辑的按钮 -1 为添加新的按钮
NSInteger editTag;
}
@property (nonatomic, strong) NSMutableArray *images;
@end
@implementation CertificateAddImageView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn = [self createButtonWithImage:nil andSeletor:@selector(addNew:)];
[self addSubview:btn];
}
return self;
}
-(NSMutableArray *)images
{
if (_images == nil) {
_images = [NSMutableArray array];
}
return _images;
}
// 添加新的控件
- (void)addNew:(UIButton *)btn
{
// 标识为添加一个新的图片
editTag = -1;
[self callImagePicker];
}
// 修改旧的控件
- (void)changeOld:(UIButton *)btn
{
editTag = btn.tag;
[self callImagePicker];
}
// 调用图片选择器
- (void)callImagePicker
{
UIImagePickerController *pc = [[UIImagePickerController alloc] init];
pc.allowsEditing = YES;
pc.delegate = self;
[self.window.rootViewController presentViewController:pc animated:YES completion:nil];
}
// 根据图片名称或者图片创建一个新的显示控件
- (UIButton *)createButtonWithImage:(UIImage *)imageNameOrImage andSeletor : (SEL)selector
{
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[addBtn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
addBtn.layer.masksToBounds = YES;
addBtn.layer.cornerRadius = 2;
addBtn.layer.borderWidth = 1;
addBtn.layer.borderColor = RGB(199, 200, 204).CGColor;
[addBtn setBackgroundColor:[UIColor whiteColor]];
addBtn.tag = self.subviews.count;

if (imageNameOrImage!=nil) {
[addBtn setImage:imageNameOrImage forState:UIControlStateNormal];

UIButton *dele = [[UIButton alloc] initWithFrame:CGRectMake(imageW-deleImageWH-20, 0, deleImageWH, deleImageWH)];
[dele setBackgroundColor:[UIColor redColor]];
[dele addTarget:self action:@selector(deletePic:) forControlEvents:UIControlEventTouchUpInside];
[addBtn addSubview:dele];
}

return addBtn;

}

// 删除图片
- (void)deletePic : (UIButton *)btn
{
[self.images removeObject:[(UIButton *)btn.superview imageForState:UIControlStateNormal]];
[btn.superview removeFromSuperview];
if ([[self.subviews lastObject] isHidden]) {
[[self.subviews lastObject] setHidden:NO];
}
if ([delegate respondsToSelector:@selector(certificateAddImageViewImages:)]) {
[delegate certificateAddImageViewImages:self.images];
}
}

// 对所有子控件进行布局
- (void)layoutSubviews
{
[super layoutSubviews];
NSUInteger count = self.subviews.count;
CGFloat btnW = imageW;
CGFloat btnH = imageW;
int maxColumn = kMaxColumn > self.frame.size.width / imageW ? self.frame.size.width / imageW : kMaxColumn;
CGFloat marginX = (self.frame.size.width - maxColumn * btnW) / (count + 1);
CGFloat marginY = marginX;
for (int i = 0; i < count; i++) {
UIButton *btn = self.subviews[i];
CGFloat btnX = (i % maxColumn) * (marginX + btnW) + marginX;
CGFloat btnY = (i / maxColumn) * (marginY + btnH) + marginY;
[UIView animateWithDuration:0.3 animations:^{
btn.frame = CGRectMake(btnX+10, btnY+10, btnW-20, btnH-20);
}];
}

}

#pragma mark - UIImagePickerController 代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
if (editTag == -1) {
// 创建一个新的控件
UIButton *btn = [self createButtonWithImage:image andSeletor:@selector(changeOld:)];
[self insertSubview:btn atIndex:self.subviews.count - 1];
[self.images addObject:image];
if (self.subviews.count - 1 == MaxImageCount) {
[[self.subviews lastObject] setHidden:YES];

}
if ([delegate respondsToSelector:@selector(certificateAddImageViewImages:)]) {
[delegate certificateAddImageViewImages:self.images];
}
}
else
{
// 根据tag修改需要编辑的控件
UIButton *btn = (UIButton *)[self viewWithTag:editTag];
NSUInteger index = [self.images indexOfObject:[btn imageForState:UIControlStateNormal]];
[self.images removeObjectAtIndex:index];
[btn setImage:image forState:UIControlStateNormal];
[self.images insertObject:image atIndex:index];
if ([delegate respondsToSelector:@selector(certificateAddImageViewImages:)]) {
[delegate certificateAddImageViewImages:self.images];
}
}
// 退出图片选择控制器
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end