博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 选取上传图片界面
阅读量:5153 次
发布时间:2019-06-13

本文共 11724 字,大约阅读时间需要 39 分钟。

1 //  2 //  MainViewController.m  3 //  AddPhoto  4 //  5 //  Created by ChenJungang on 14/11/20.  6 //  Copyright (c) 2014年 ChenJungang. All rights reserved.  7 //  8   9 #import "MainViewController.h" 10 #import "CustomFlowLayout.h" 11 #import "PhotoCell.h" 12 #import "ResuableView.h" 13 #import 
14 #import "DetailViewController.h" 15 16 17 #define kImage @"kImage" 18 #define kImageData @"kImageData" 19 #define kImageReferenceUrl @"kImageReferenceUrl" 20 21 22 static NSString *itemIdentifier = @"ItemIdentifier"; 23 static NSString *footerReuseId = @"footerReuseId"; 24 25 @interface MainViewController ()
26 27 @property (strong, nonatomic)UICollectionView *collectionView; 28 @property (strong, nonatomic)CustomFlowLayout *customFlowLayout; 29 @property (strong, nonatomic)NSMutableArray *imageArray; 30 @property (strong, nonatomic)NSIndexPath *indexPath; 31 32 @end 33 34 @implementation MainViewController 35 36 - (instancetype)init{ 37 self = [super init]; 38 if (self) { 39 _imageArray = [NSMutableArray array]; 40 } 41 return self; 42 } 43 44 - (void)viewDidLoad { 45 [super viewDidLoad]; 46 // Do any additional setup after loading the view from its nib. 47 self.title = @"add photo"; 48 49 _customFlowLayout = [CustomFlowLayout new]; 50 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 64, [[UIScreen mainScreen] bounds].size.width - 20, 60) 51 collectionViewLayout:_customFlowLayout]; 52 _collectionView.backgroundColor = [UIColor whiteColor]; 53 54 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { 55 self.edgesForExtendedLayout = UIRectEdgeNone; 56 } 57 58 [self.collectionView registerClass:[PhotoCell class] 59 forCellWithReuseIdentifier:itemIdentifier]; 60 61 [self.collectionView registerClass:[ResuableView class] 62 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter 63 withReuseIdentifier:footerReuseId]; 64 65 _collectionView.dataSource = self; 66 _collectionView.delegate = self; 67 68 [self.view addSubview:_collectionView]; 69 } 70 71 #pragma mark - UICollectionViewDataSource 72 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 73 { 74 return [_imageArray count]; 75 } 76 77 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 78 { 79 PhotoCell *cell = \ 80 [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier 81 forIndexPath:indexPath]; 82 83 if ([_imageArray count] != 0) { 84 cell.showImageView.image = _imageArray[indexPath.row][kImage]; 85 } 86 87 return cell; 88 } 89 90 #pragma mark - UICollectionViewDelegate 91 92 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 93 NSLog(@"row : %d",indexPath.row); 94 95 96 _indexPath = indexPath; 97 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"查看", nil]; 98 [actionSheet showInView:self.view]; 99 100 }101 102 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{103 104 ResuableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter105 withReuseIdentifier:footerReuseId106 forIndexPath:indexPath];107 _indexPath = indexPath;108 footerView.delegate = self;109 110 return footerView;111 }112 113 #pragma mark - UIActionSheetDelegate114 115 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{116 117 switch (buttonIndex) {118 case 0:{119 if ([_imageArray count] != 0) {120 [_imageArray removeObjectAtIndex:_indexPath.row];121 [self.collectionView reloadData];122 }123 break;124 }125 case 1:{126 DetailViewController *detailVC = [[DetailViewController alloc] init];127 detailVC.image = _imageArray[_indexPath.row][kImage];128 [self presentViewController:detailVC animated:YES completion:nil];129 break;130 }131 default:132 break;133 }134 }135 136 137 #pragma mark - UIImagePickerControllerDelegate138 139 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{140 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {141 142 if ([_imageArray count] != 0) {143 for (NSDictionary *dic in _imageArray) {144 if ([[info objectForKey:UIImagePickerControllerReferenceURL] isEqual:[dic objectForKey:kImageReferenceUrl]]) {145 return [self dismissViewControllerAnimated:YES completion:^{146 [MBHUDHelper showWarningWithText:@"该图片已经选择"];147 }];148 }149 }150 }151 //取出选中图片的信息152 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];153 NSData *imageData = UIImageJPEGRepresentation(image, 1.0);154 NSString *referenceUrl = [info objectForKey:UIImagePickerControllerReferenceURL];155 156 NSDictionary *imageInfoDic = @{kImage:image, kImageData:imageData, kImageReferenceUrl:referenceUrl};157 158 [_imageArray addObject:imageInfoDic];159 [self.collectionView reloadData];160 161 //滚动到最右边162 if ([_imageArray count] != 0) {163 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[_imageArray indexOfObject:[_imageArray lastObject]] inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];164 }165 }166 [picker dismissViewControllerAnimated:YES completion:nil];167 }168 169 170 #pragma mark - ResuableViewDelegate171 172 - (void)clickAddPhotoBtnAction{173 [self getPhotoFromPhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];174 }175 176 -(void)getPhotoFromPhotoLibrary:(UIImagePickerControllerSourceType)sourceType177 {178 NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];179 if([UIImagePickerController isSourceTypeAvailable:sourceType] && mediaTypes.count > 0)180 {181 NSArray *mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];182 UIImagePickerController *picker = [[UIImagePickerController alloc] init];183 picker.mediaTypes = mediaTypes;184 picker.delegate = self;185 picker.allowsEditing = YES;186 picker.sourceType = sourceType;187 NSString *requiredMediaType = (NSString *)kUTTypeImage;188 NSArray *arrMediaTypes = [NSArray arrayWithObject:requiredMediaType];189 [picker setMediaTypes:arrMediaTypes];190 [self presentViewController:picker animated:YES completion:nil];191 }else{192 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];193 [alert show];194 }195 }196 197 - (void)didReceiveMemoryWarning {198 [super didReceiveMemoryWarning];199 // Dispose of any resources that can be recreated.200 }201 202 203 @end
MainViewController.m
1 // 2 //  CustomFlowLayout.m 3 //  AddPhoto 4 // 5 //  Created by ChenJungang on 14/11/20. 6 //  Copyright (c) 2014年 ChenJungang. All rights reserved. 7 // 8  9 #import "CustomFlowLayout.h"10 11 @implementation CustomFlowLayout12 - (instancetype)init13 {14     self = [super init];15     if (self)16     {17         self.itemSize                = CGSizeMake(50, 50);                  // 单元格尺寸18         self.sectionInset            = UIEdgeInsetsMake(0, 0, 0, 0);        // 单元格边缘19         self.minimumInteritemSpacing = 2.0f;                                // 横排单元格最小间隔20         self.minimumLineSpacing      = 2.0f;                                // 单元格最小行间距21         self.scrollDirection = UICollectionViewScrollDirectionHorizontal;   // 滚动方向22         self.footerReferenceSize     = CGSizeMake(50, 50);                  // footerView 的尺寸23         24     }25     return self;26 }27 28 @end
CustomFlowLayout.m
1 // 2 //  PhotoCell.m 3 //  AddPhoto 4 // 5 //  Created by ChenJungang on 14/11/20. 6 //  Copyright (c) 2014年 ChenJungang. All rights reserved. 7 // 8  9 #import "PhotoCell.h"10 11 @implementation PhotoCell12 13 - (void)awakeFromNib {14     // Initialization code15 }16 17 - (id)initWithFrame:(CGRect)frame18 {19     self = [super initWithFrame:frame];20     if (self)21     {22         self.backgroundColor = [UIColor whiteColor];23         24         CGRect rect = self.bounds;25         rect.origin.x    += 3;26         rect.origin.y    += 3;27         rect.size.height -= 6;28         rect.size.width  -= 6;29         30         _showImageView = [[UIImageView alloc] initWithFrame:rect];31         _showImageView.contentMode = UIViewContentModeScaleAspectFill;32         33         _showImageView.layer.masksToBounds = YES;34         [self addSubview:_showImageView];35     }36     37     return self;38 }39 40 @end
PhotoCell.m
1 // 2 //  ResuableView.h 3 //  AddPhoto 4 // 5 //  Created by ChenJungang on 14/11/20. 6 //  Copyright (c) 2014年 ChenJungang. All rights reserved. 7 // 8  9 #import 
10 11 @protocol ResuableViewDelegate
12 13 - (void)clickAddPhotoBtnAction;14 15 @end16 17 @interface ResuableView : UICollectionReusableView18 19 @property (nonatomic, strong) id
delegate;20 21 @end
ResuableView.h
1 // 2 //  ResuableView.m 3 //  AddPhoto 4 // 5 //  Created by ChenJungang on 14/11/20. 6 //  Copyright (c) 2014年 ChenJungang. All rights reserved. 7 // 8  9 #import "ResuableView.h"10 11 @interface ResuableView()12 @property (nonatomic, strong) UIButton  *addPhotoBtn;13 @end14 15 @implementation ResuableView16 17 -(instancetype)initWithFrame:(CGRect)frame{18     self = [super initWithFrame:frame];19     if (self) {20         21         self.backgroundColor = [UIColor whiteColor];22         23         CGRect rect = self.bounds;24         rect.origin.x    += 3;25         rect.origin.y    += 6;26         rect.size.height -= 12;27         rect.size.width  -= 6;28         29         _addPhotoBtn = [[UIButton alloc] initWithFrame:rect];30         _addPhotoBtn.contentMode = UIViewContentModeScaleAspectFill;31         [_addPhotoBtn setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal];32         _addPhotoBtn.layer.masksToBounds = YES;33         [self addSubview:_addPhotoBtn];34         35         [_addPhotoBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];36         37     }38     return self;39 }40 41 - (void)buttonAction:(id)sender{42     if ([_delegate respondsToSelector:@selector(clickAddPhotoBtnAction)]) {43         [_delegate clickAddPhotoBtnAction];44     }45 }46 47 @end
ResuableView.m

 

转载于:https://www.cnblogs.com/ablettchen/p/4115349.html

你可能感兴趣的文章
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
Hdu - 1002 - A + B Problem II
查看>>
Android设置Gmail邮箱
查看>>
js编写时间选择框
查看>>
JIRA
查看>>
小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
查看>>
深浅拷贝(十四)
查看>>
HDU 6370(并查集)
查看>>
BZOJ 1207(dp)
查看>>
HDU 2076 夹角有多大(题目已修改,注意读题)
查看>>
洛谷P3676 小清新数据结构题(动态点分治)
查看>>
九校联考-DL24凉心模拟Day2T1 锻造(forging)
查看>>
Attributes.Add用途与用法
查看>>