类似微信选择添加删除图片的demo
仿写了一个类似微信选择添加图片的demo,非常容易添加到工程当中,github地址https://github.com/wubianxiaoxian/AddPicture,主要用到了collectionview
1 // 2 // ViewController.m 3 // KevinCollectionView 4 // 5 // Created by Kevin Sun on 15/12/14. 6 // Copyright © 2015年 Kevin Sun. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "CollectionViewCell.h" 11 #import "KevinPreViewController.h" 12 13 static NSMutableArray *currentImages; 14 15 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{ 16 UICollectionView *_collectionView; 17 18 } 19 20 21 @end 22 23 @implementation ViewController 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 self.view.backgroundColor=[UIColor whiteColor]; 27 UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; 28 layout.minimumLineSpacing = 0.0f; 29 layout.minimumInteritemSpacing = 0.0f; 30 31 _collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout]; 32 // [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) { 33 // make.left.right.top.bottom.mas_equalTo(0); 34 // }]; 35 self.navigationController.navigationBar.barTintColor=[UIColor clearColor]; 36 37 [self.view addSubview:_collectionView]; 38 _collectionView.backgroundColor=[UIColor whiteColor]; 39 _collectionView.delegate=self; 40 _collectionView.dataSource=self; 41 [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"]; 42 if(currentImages ==nil) 43 { 44 currentImages=[[NSMutableArray alloc] init]; 45 } 46 47 48 49 // Do any additional setup after loading the view, typically from a nib. 50 } 51 -(void)viewWillAppear:(BOOL)animated{ 52 [_collectionView reloadData]; 53 } 54 #pragma mark -UIColletionDelegate 55 //指定setion个数 56 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 57 return 1; 58 } 59 //指定section中的colletionviewCELL的个数 60 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 61 return currentImages.count==0?1:currentImages.count+1; 62 } 63 //配置section中的collectionviewcell的展示 64 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 65 CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 66 // cell.backgroundColor=[UIColor redColor]; 67 68 UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/4-10, self.view.frame.size.width/4-10)]; 69 NSLog(@"currentImages.count %ld",currentImages.count); 70 for(UIView *view in [cell.contentView subviews]) 71 { 72 [view removeFromSuperview]; 73 } 74 if(currentImages.count==0||indexPath.row==currentImages.count) 75 { NSLog(@"我加载了设置加好图片"); 76 imageView.image=nil; 77 // [cell.contentView removeFromSuperview:imageView]; 78 imageView.image=[UIImage imageNamed:@"ico.ooopic.com.png"]; 79 } 80 else{ 81 NSLog(@"我加载了设置背景图片"); 82 83 while ([cell.contentView.subviews lastObject] != nil) { 84 [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; 85 } 86 imageView.image=currentImages[indexPath.row]; 87 } 88 89 // imageView.contentMode=UIViewContentModeScaleAspectFill; 90 [cell.contentView addSubview:imageView]; 91 return cell; 92 return cell; 93 94 } 95 //指定单元格的大小 96 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 97 return CGSizeMake(self.view.frame.size.width/4-10, self.view.frame.size.width/4-10); 98 99 } 100 ////设置每个cell上下左右相距 101 //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ 102 // return UIEdgeInsetsMake(0, 0, 0, 0); 103 //} 104 ////设置最小行间距 也就是前一行和后一行的中间最小间隔 105 //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ 106 // return 0.0f; 107 //} 108 //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ 109 // return 0.0f; 110 //} 111 #pragma mark 点击cell 112 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 113 if(indexPath.row==currentImages.count) 114 { 115 UIActionSheet *action=[[UIActionSheet alloc] initWithTitle:@"选取照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从摄像头选取", @"从图片库选择",nil]; 116 [action showInView:self.view]; 117 } 118 else 119 { 120 [KevinPreViewController setPreviewImage:currentImages[indexPath.row]]; 121 [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"PreviewVC"] animated:YES]; 122 } 123 124 } 125 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 126 [picker dismissViewControllerAnimated:YES completion:nil]; 127 UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage]; 128 NSData *tempData=UIImageJPEGRepresentation(image, 0.5f); 129 image=[UIImage imageWithData:tempData]; 130 if(currentImages ==nil) 131 { 132 currentImages=[[NSMutableArray alloc] init]; 133 } 134 [currentImages addObject:image]; 135 [_collectionView reloadData]; 136 // [self saveImage:image withName:@""] 137 } 138 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ 139 [picker dismissViewControllerAnimated:YES completion:nil]; 140 } 141 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 142 143 switch (buttonIndex) { 144 case 0: 145 [self openCamera]; 146 break; 147 case 1: 148 [self openLibary]; 149 break; 150 default: 151 break; 152 } 153 } 154 -(void)openCamera{ 155 //UIImagePickerControllerSourceType *type=UIImagePickerControllerSourceTypeCamera; 156 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 157 { 158 UIImagePickerController *picker=[[UIImagePickerController alloc] init]; 159 picker.delegate=self; 160 picker.sourceType=UIImagePickerControllerSourceTypeCamera; 161 picker.allowsEditing=YES; 162 [self presentViewController:picker animated:YES completion:nil]; 163 164 } 165 } 166 -(void)openLibary{ 167 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 168 { 169 UIImagePickerController *picker=[[UIImagePickerController alloc] init]; 170 picker.delegate=self; 171 picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 172 picker.allowsEditing=YES; 173 [self presentViewController:picker animated:YES completion:nil]; 174 175 } 176 177 } 178 -(void) saveImage:(UIImage *)image withName:(NSString *)name 179 { 180 NSData *imageData=UIImageJPEGRepresentation(image, 0.5); 181 NSString *path=[NSTemporaryDirectory() stringByAppendingPathComponent:name]; 182 [imageData writeToFile:path atomically:YES]; 183 184 } 185 +(void)deleteSelectedImage:(NSInteger)index 186 { 187 if(currentImages!=nil) 188 [currentImages removeObjectAtIndex:index]; 189 } 190 +(void)deleteSelectedImageWithImage:(UIImage *)image{ 191 if(currentImages!=nil) 192 [currentImages removeObject:image]; 193 194 } 195 196 - (void)didReceiveMemoryWarning { 197 [super didReceiveMemoryWarning]; 198 // Dispose of any resources that can be recreated. 199 } 200 201 @end
github地址 https://github.com/wubianxiaoxian/AddPicture