IOS研究院之打开照相机与本地相册选择图片
如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样。最终点击发送将按钮将图片2进制图片上传服务器。
下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。
AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。
1
|
#import <UIKit/UIKit.h>
|
2
|
#import "TestViewController.h"
|
3
|
4
|
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
5
|
6
|
@property (strong, nonatomic) UIWindow *window;
|
7
|
@property (strong, nonatomic) UINavigationController *navController;
|
8
|
@property (strong, nonatomic) UIViewController *viewController;
|
9
|
@end
|
AppDelegate.m 在这里就是打开我们创建的TestViewController
01
|
#import "AppDelegate.h"
|
02
|
03
|
@implementation AppDelegate
|
04
|
05
|
@synthesize window = _window;
|
06
|
@synthesize navController;
|
07
|
@synthesize viewController;
|
08
|
09
|
- ( void )dealloc
|
10
|
{
|
11
|
[_window release];
|
12
|
[super dealloc];
|
13
|
}
|
14
|
15
|
- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
16
|
{
|
17
|
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
|
18
|
19
|
self.window.backgroundColor = [UIColor whiteColor];
|
20
|
self.viewController = [[TestViewController alloc]init];
|
21
|
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
|
22
|
[self.window addSubview:navController.view];
|
23
|
24
|
[self.window makeKeyAndVisible];
|
25
|
return YES;
|
26
|
}
|
27
|
28
|
@end
|
TestViewController.h 注意这里面引入了很多代理类。
01
|
#import <UIKit/UIKit.h>
|
02
|
03
|
@interface TestViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
|
04
|
{
|
05
|
//输入框
|
06
|
UITextView *_textEditor;
|
07
|
08
|
//下拉菜单
|
09
|
UIActionSheet *myActionSheet;
|
10
|
11
|
//图片2进制路径
|
12
|
NSString* filePath;
|
13
|
}
|
14
|
@end
|
TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。
001
|
#import "TestViewController.h"
|
002
|
003
|
@interface TestViewController ()
|
004
|
005
|
@end
|
006
|
007
|
@implementation TestViewController
|
008
|
009
|
- ( void )viewDidLoad
|
010
|
{
|
011
|
[super viewDidLoad];
|
012
|
//导航栏标题
|
013
|
self.navigationItem.title = @ "雨松MOMO输入框" ;
|
014
|
015
|
//导航栏按钮
|
016
|
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
|
017
|
initWithTitle: @ "发送"
|
018
|
style: UIBarButtonItemStyleDone
|
019
|
target: self
|
020
|
action: @selector(sendInfo)] autorelease];
|
021
|
022
|
//输入框显示区域
|
023
|
_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
|
024
|
//设置它的代理
|
025
|
_textEditor.delegate = self;
|
026
|
_textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
027
|
_textEditor.keyboardType = UIKeyboardTypeDefault;
|
028
|
_textEditor.font = [UIFont systemFontOfSize:20];
|
029
|
_textEditor.text = @ "请输入内容" ;
|
030
|
031
|
//默认软键盘是在触摸区域后才会打开
|
032
|
//这里表示进入当前ViewController直接打开软键盘
|
033
|
[_textEditor becomeFirstResponder];
|
034
|
035
|
//把输入框加在视图中
|
036
|
[self.view addSubview:_textEditor];
|
037
|
038
|
//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
|
039
|
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@ "camera" ofType:@ "png" ]];
|
040
|
041
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
042
|
button.frame = CGRectMake(0, 120, image.size.width, image.size.height);
|
043
|
044
|
[button setImage:image forState:UIControlStateNormal];
|
045
|
046
|
[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
|
047
|
048
|
//把它也加在视图当中
|
049
|
[self.view addSubview:button];
|
050
|
051
|
}
|
052
|
053
|
-( void )openMenu
|
054
|
{
|
055
|
//在这里呼出下方菜单按钮项
|
056
|
myActionSheet = [[UIActionSheet alloc]
|
057
|
initWithTitle:nil
|
058
|
delegate:self
|
059
|
cancelButtonTitle:@ "取消"
|
060
|
destructiveButtonTitle:nil
|
061
|
otherButtonTitles: @ "打开照相机" , @ "从手机相册获取" ,nil];
|
062
|
063
|
[myActionSheet showInView:self.view];
|
064
|
[myActionSheet release];
|
065
|
066
|
}
|
067
|
068
|
- ( void )actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
|
069
|
{
|
070
|
071
|
//呼出的菜单按钮点击后的响应
|
072
|
if (buttonIndex == myActionSheet.cancelButtonIndex)
|
073
|
{
|
074
|
NSLog(@ "取消" );
|
075
|
}
|
076
|
077
|
switch (buttonIndex)
|
078
|
{
|
079
|
case 0: //打开照相机拍照
|
080
|
[self takePhoto];
|
081
|
break ;
|
082
|
083
|
case 1: //打开本地相册
|
084
|
[self LocalPhoto];
|
085
|
break ;
|
086
|
}
|
087
|
}
|
088
|
089
|
//开始拍照
|
090
|
-( void )takePhoto
|
091
|
{
|
092
|
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
|
093
|
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
|
094
|
{
|
095
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
096
|
picker.delegate = self;
|
097
|
//设置拍照后的图片可被编辑
|
098
|
picker.allowsEditing = YES;
|
099
|
picker.sourceType = sourceType;
|
100
|
[picker release];
|
101
|
[self presentModalViewController:picker animated:YES];
|
102
|
} else
|
103
|
{
|
104
|
NSLog(@ "模拟其中无法打开照相机,请在真机中使用" );
|
105
|
}
|
106
|
}
|
107
|
108
|
//打开本地相册
|
109
|
-( void )LocalPhoto
|
110
|
{
|
111
|
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
|
112
|
113
|
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
|
114
|
picker.delegate = self;
|
115
|
//设置选择后的图片可被编辑
|
116
|
picker.allowsEditing = YES;
|
117
|
[self presentModalViewController:picker animated:YES];
|
118
|
[picker release];
|
119
|
}
|
120
|
121
|
//当选择一张图片后进入这里
|
122
|
-( void )imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
123
|
124
|
{
|
125
|
126
|
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
|
127
|
128
|
//当选择的类型是图片
|
129
|
if ([type isEqualToString:@ "public.image" ])
|
130
|
{
|
131
|
//先把图片转成NSData
|
132
|
UIImage* image = [info objectForKey:@ "UIImagePickerControllerOriginalImage" ];
|
133
|
NSData *data;
|
134
|
if (UIImagePNGRepresentation(image) == nil)
|
135
|
{
|
136
|
data = UIImageJPEGRepresentation(image, 1.0);
|
137
|
}
|
138
|
else
|
139
|
{
|
140
|
data = UIImagePNGRepresentation(image);
|
141
|
}
|
142
|
143
|
//图片保存的路径
|
144
|
//这里将图片放在沙盒的documents文件夹中
|
145
|
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@ "Documents" ];
|
146
|
147
|
//文件管理器
|
148
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
149
|
150
|
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
|
151
|
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
|
152
|
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@ "/image.png" ] contents:data attributes:nil];
|
153
|
154
|
//得到选择后沙盒中图片的完整路径
|
155
|
filePath = [[NSString alloc]initWithFormat:@ "%@%@" ,DocumentsPath, @ "/image.png" ];
|
156
|
157
|
//关闭相册界面
|
158
|
[picker dismissModalViewControllerAnimated:YES];
|
159
|
160
|
//创建一个选择后图片的小图标放在下方
|
161
|
//类似微薄选择图后的效果
|
162
|
UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
|
163
|
CGRectMake(50, 120, 40, 40)] autorelease];
|
164
|
165
|
smallimage.image = image;
|
166
|
//加在视图中
|
167
|
[self.view addSubview:smallimage];
|
168
|
169
|
}
|
170
|
171
|
}
|
172
|
173
|
- ( void )imagePickerControllerDidCancel:(UIImagePickerController *)picker
|
174
|
{
|
175
|
NSLog(@ "您取消了选择图片" );
|
176
|
[picker dismissModalViewControllerAnimated:YES];
|
177
|
}
|
178
|
179
|
-( void )sendInfo
|
180
|
{
|
181
|
NSLog(@ "图片的路径是:%@" , filePath);
|
182
|
183
|
NSLog(@ "您输入框中的内容是:%@" , _textEditor.text);
|
184
|
}
|
185
|
186
|
- ( void )viewDidUnload
|
187
|
{
|
188
|
[super viewDidUnload];
|
189
|
// Release any retained subviews of the main view.
|
190
|
}
|
191
|
192
|
- ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
|
193
|
{
|
194
|
return (interfaceOrientation == UIInterfaceOrientationPortrait);
|
195
|
}
|
196
|
197
|
@end
|
如下图所示,打开下拉菜单按钮开始选择打开相机 或者 打开本地相册。模拟器中是无法打开照相机的的,切记。
如下图所示,这里就是我本地的相册啦,里面保存了几张图片,选择一张即可。
我在这里再说说图片上传, 图片上传我们采用的是2进制ASIHTTPRequest 来完成的。
发送请求
01
|
NSString *server_base = [NSString stringWithFormat:@ "%@/users/uploadResource.json" , _server];
|
02
|
03
|
ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];
|
04
|
05
|
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:server_base]];
|
06
|
07
|
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: NO];
|
08
|
[request setDelegate :self];
|
09
|
[request setDidFinishSelector:@selector(sendCommentSucc:)];
|
10
|
[request setDidFailSelector:@selector(sendCommentFail:)];
|
11
|
// res 就是 需要上传图片文件的路径
|
12
|
[request setFile:res forKey:@ "res" ];
|
13
|
14
|
[queue addOperation:request];
|
15
|
[queue go];
|
最后是文本的源码下载地址:http://vdisk.weibo.com/s/accm9
IOS研究院之打开照相机与本地相册选择图片相关推荐
- iOS打开照相机与本地相册选择图片
最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...
- iOS 打开照相机与本地相册选择图片
最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...
- unity 启动相机_Unity3D研究院之打开照相机与本地相册进行裁剪显示(三十三)...
最近做项目需要用到这个功能,就是在Unity中调用Android本地相册或直接打开摄像机拍照并且裁剪一部分用于用户头像,今天研究了一下,那么研究出成果了MOMO一定要分享给大家.Unity与Andro ...
- uniapp 本地相册选择图片
标题 uniapp 本地相册选择图片 export default {data(){return {imageLists:[]}} }methods:{delete(index){ // 获取该数组的 ...
- uni-app 从本地相册选择图片或使用相机拍照
终于弄懂这个选择图片的流程,删除了很多不需要的地方,最终得到的就是这个版本 页面部分 <view>上传图片 {{imageList.length}}/{{count}}</view& ...
- 微信小程序:从本地相册选择图片、相机拍照,并将所选图片上传到服务器
简介 小程序项目中有个需求:用户聊天时可以上传图片,还能支持拍照.于是学习了一下,分享给大家. 微信小程序中有个wx.chooseImage的方法可以快捷调用达到需求的效果. 具体实现 1.为了简化代 ...
- 微信小程序:选择从本地相册选择图片或使用相机拍照
先弹出操作菜单,选择拍照还是从相册选择.根据用户选择进行相应操作.如图所示 代码如下: browse:function(){let that = this;wx.showActionSheet({it ...
- android从本地相册选择图片uri三星手机适配问题
转载地址:http://blog.csdn.net/CathyChen0910/article/details/62456438 启动系统相册intent Intent intentFromGalle ...
- 微信小程序chooseImage(从本地相册选择图片或使用相机拍照)
一.使用API wx.chooseImage(OBJECT) var util = require('../../utils/util.js') Page({data:{src:"../im ...
最新文章
- HTTP/2做错了什么?刚刚辉煌2年就要被弃用了!?
- 用Kotlin写Android Gradle脚本
- 函数的作用域(嵌套函数的运行)
- 初入react -01
- 【中级软考】结构化开发方法是啥,主要包含哪些内容?
- k8s多节点仪表盘(web界面)部署与谷歌浏览器访问k8s仪表盘问题解决!
- 如何取消IE“已限制此网页运行可以访问计算机的脚本或ActiveX控件”(小技巧)...
- 余额宝利率破2.4%,你还会把钱放在余额宝里面吗?
- Flowable 数据库表结构 ACT_RU_JOB
- 方格取数(1)(HDU-1565)
- 计算机科学与技术教师简介,清华大学计算机科学与技术系导师教师师资介绍简介-赵 颖...
- 爬虫3 requests基础之下载图片用content(二进制内容)
- 简述html文件的基本标记组成_HTML是什么呢?
- c语言中使用相对路径
- PVID、Access、Trunk、Hybrid三种不同端口收发规则、Vlan中tagged端口和untagged端口的区别
- MKVToolNix v7.4.0 正式版
- 特征检测和特征提取算子
- 用友u8服务器修改ipv4,用友U8-OA11.1 用友U8加密狗更换服务器了-用友U8
- Centos8关闭防火墙
- Backend For Frontend 实践心得