iOS选择照片九宫格动态布局 发表于 2017-11-27 | 直接上源码 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150//// 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