标签:
// // ViewController.m // text // // Created by 123 on 16/4/29. // Copyright © 2016年 yipinbaike. All rights reserved. // #import "ViewController.h" #define IMAGEVIEW_NUM_MAX 5 @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate> { NSMutableArray * _imageViewArray; NSInteger _currentIndex; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; _imageViewArray = [[NSMutableArray alloc]init]; UIImageView * imageView = [[UIImageView alloc]init]; imageView.image = [UIImage imageNamed:@"请求失败"]; [_imageViewArray addObject:imageView]; [self.view addSubview:imageView]; [self updateImageViewsFrame]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==2) { return; } UIImagePickerController * picker = [[UIImagePickerController alloc]init]; picker.delegate = self; picker.allowsEditing = YES; if (buttonIndex==0&&[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { picker.sourceType =UIImagePickerControllerSourceTypeCamera; } else if (buttonIndex==1&&[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } [self presentViewController:picker animated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImageView * currentImageView =(UIImageView*)_imageViewArray[_currentIndex]; currentImageView.image = info[UIImagePickerControllerEditedImage]; [picker dismissViewControllerAnimated:YES completion:nil]; UIButton * delBtn = [[UIButton alloc]initWithFrame:CGRectMake(currentImageView.frame.size.width-20, -10, 30,30)]; [currentImageView addSubview:delBtn]; [delBtn addTarget:self action:@selector(delBtnTouched:) forControlEvents:UIControlEventTouchUpInside]; delBtn.backgroundColor = [UIColor blackColor]; if (_imageViewArray.count<IMAGEVIEW_NUM_MAX&&_currentIndex==_imageViewArray.count-1) { UIImageView * imageView = [[UIImageView alloc]init]; imageView.image = [UIImage imageNamed:@"请求失败"]; [self.view addSubview:imageView]; [_imageViewArray addObject:imageView]; [self updateImageViewsFrame]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)updateImageViewsFrame{ CGFloat imageWeight = 100; CGFloat imageHight = 100; CGFloat sepW = (self.view.frame.size.width - imageWeight*3-20*2)/2; CGFloat sepH = 20.0f; for (int i = 0; i<_imageViewArray.count; i++) { UIImageView * imageView= _imageViewArray[i]; NSInteger row = i/3;//计算行数 NSInteger que = i%3; //计算列数 imageView.frame = CGRectMake(20+que*(imageWeight+sepW), 30+row*(imageHight+sepH), imageWeight, imageHight); imageView.tag = i; UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTouched:)]; [imageView addGestureRecognizer:ges]; imageView.userInteractionEnabled = YES; for (UIView * view in imageView.subviews) { if ([view isKindOfClass:[UIButton class]]) { view.tag = i; } } } } - (void)imageViewTouched:(UIGestureRecognizer*)ges{ UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil]; [actionSheet showInView:self.view]; actionSheet.cancelButtonIndex = 2; _currentIndex = ges.view.tag; } - (void)delBtnTouched:(UIButton*)btn{ [((UIImageView*)_imageViewArray[btn.tag]) removeFromSuperview]; [_imageViewArray removeObjectAtIndex:btn.tag]; [self updateImageViewsFrame]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
读取相册或者相机多张图片,同时按照一定的要求排列,并有删除添加功能
标签:
原文地址:http://www.cnblogs.com/fusheng-it/p/5458779.html