iOS 选取上传图片界面

  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 <MobileCoreServices/MobileCoreServices.h>
 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 ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ResuableViewDelegate,UIActionSheetDelegate>
 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:UICollectionElementKindSectionFooter
105                                                                   withReuseIdentifier:footerReuseId
106                                                                          forIndexPath:indexPath];
107     _indexPath = indexPath;
108     footerView.delegate = self;
109
110     return footerView;
111 }
112
113 #pragma mark - UIActionSheetDelegate
114
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 - UIImagePickerControllerDelegate
138
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 - ResuableViewDelegate
171
172 - (void)clickAddPhotoBtnAction{
173     [self getPhotoFromPhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
174 }
175
176 -(void)getPhotoFromPhotoLibrary:(UIImagePickerControllerSourceType)sourceType
177 {
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 CustomFlowLayout
12 - (instancetype)init
13 {
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 PhotoCell
12
13 - (void)awakeFromNib {
14     // Initialization code
15 }
16
17 - (id)initWithFrame:(CGRect)frame
18 {
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 <UIKit/UIKit.h>
10
11 @protocol ResuableViewDelegate <NSObject>
12
13 - (void)clickAddPhotoBtnAction;
14
15 @end
16
17 @interface ResuableView : UICollectionReusableView
18
19 @property (nonatomic, strong) id<ResuableViewDelegate>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 @end
14
15 @implementation ResuableView
16
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

时间: 2024-11-09 07:22:08

iOS 选取上传图片界面的相关文章

iOS与H5界面JSBridge交互Demo

iOS与H5界面JSBridge交互Demo 最近公司需要加活动和新闻模块, boss看同样的设计稿, 我们iOS做一遍, 安卓做一遍, 小程序又做一遍; 所以决定用H5页面. 但我们Native不仅仅加载URL就行, 还需要跟H5有交互, 安卓大哥跟我慢慢填坑- 我用了一个library(GCWebviewJSBridge-iOS), github网址:github.com/wheying/GCWebviewJSBridge-iOS 他的Demo不太容易看得懂, 看得我一脸懵逼, 我写了一个简

【新手--android日记】实现IOS风格电话界面

[前言--新手日记] 开始学习android开发,通过做一个通讯录练习,打算实现各种自己想实现的功能. 新手作品,技术含量很浅.主要是记录自己的学习过程. 纯学习之用,求评论,求建议,求教导. [正题] 一.下了好多通话软件,感觉都不怎么样,表示还是比较喜欢原来的QQ通讯录.现在换成微信通讯录,没以前的感觉好. 最后还是倾向于选择IOS的电话界面. 先附图: IOS的:       元器件很简单,主要问题还是在布局方面. 分底部和顶部两部分说吧. 二.底部设计: 底部设计,RadioGroup加

推荐一个iOS应用UI界面设计站点

Patterns是一个分享ios应用UI界面的站点,专注于分享iOS应用UI界面的细节.依照设计元素进行分类,依照iOS经常使用功能对各类UI进行分类展示. 链接:url=http%3A%2F%2Fwww.patternsofdesign.co.uk%2F&link2key=fa4fb02efb" target="_blank" rel="nofollow" style="color:rgb(173,98,85); text-decora

推荐一个iOS应用UI界面设计网站

定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令. 在 HTML 4.01 中,<!DOCTYPE> 声明引用 DTD,因为 HTML 4.01 基于 SGML.DTD 规定了标记语言的规则,这样浏览器才能正确地呈现内容. DOCTYPE   3种类型 HTML 4.01 Strict 该

用IOS做一个界面切换的效果(登录界面和注册界面和找回密码界面的切换)(用封装好的lable和textf创建界面)

创建一个类封装uitextfield和UIlabel (源代码.m文件) #import "TLView.h" @interface TLView () { UILabel *_desLabel;    //左边的lable UITextField *_textField;//右边的 } @end @implementation TLView //改写父类的初始化方法,处理相同的性能 - (id)initWithFrame:(CGRect)frame { self = [super i

iOS仿QQ界面

iOS仿QQ界面 仿制QQ5.0的界面,可以切换主题,并且有左右滑动特效. 下载地址:http://www.dwz.cn/z08ik 源码运行截图

iOS选取相册中iCloud云上图片和视频的处理

关于iOS选取相册中iCloud云上图片和视频 推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因,需要处理接入一个视频模块,在视频选择的时候遇到了一个不太容易发现的bug,产生的原因是由于手机内存小,而用户又打开了相册同步iCloud, 加载中的图片 在这时,如果本地可用内存过小,会导致将本地相册中的图片或视频删除只留缩略图,如果App调用的时候想要选取这种图片就需要从iCloud云中进行下载,才

iOS interface guidelines (界面设计指南)&lt;一&gt;

一.      为iOS而设计 1.iOS体现的主题: (1)Deference(顺从):UI的存在就是为了让顾客更加容易理解和进行交互,而不是要和顾客玩智力游戏 (2)Clarity(清晰):在每个尺寸中都能清晰的显示文字,表达精确和图像清晰的图标,装饰物要合适且搭配自然,设计的功能要击中一点,表达明确          (3)Depth(深度):生动的视觉和真实的感情能够让界面充满生气,也可以提高用户的兴趣和使用户更加容易理解 无论你是想要重新设计旧app的界面还是创造一个,你都需要考虑下面

iOS Post上传图片, 文件流的形式

最近看到群里很多小伙伴有问上传图片服务器收不到, 解析不了.  可以说就一个属性  ContentType , 不论你使用第三方的AFN, 还是自己封装NSURLSesstion. 都需要把ContentType 设置和后台的解析方式一样(可能术语不是很标准, 总之就这个意思哈,谅解) 下面贴一段AFN的上传图片代码吧, 亲测有效, 不过可能只是针对我的服务器.  只要和后台同学沟通好就行了 //此body是向后台传的参数, 因为是上传图片, 所以只给个图片名就够了, 这个和后台去问 NSDic