如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样。最终点击发送将按钮将图片2进制图片上传服务器。

下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。
AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。
1 |
#import <UIKit/UIKit.h> |
2 |
#import "TestViewController.h" |
4 |
@interface AppDelegate : UIResponder <UIApplicationDelegate> |
6 |
@property (strong, nonatomic) UIWindow *window; |
7 |
@property (strong, nonatomic) UINavigationController *navController; |
8 |
@property (strong, nonatomic) UIViewController *viewController; |
AppDelegate.m 在这里就是打开我们创建的TestViewController
01 |
#import "AppDelegate.h" |
03 |
@implementation AppDelegate |
05 |
@synthesize window = _window; |
06 |
@synthesize navController; |
07 |
@synthesize viewController; |
15 |
- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
17 |
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
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]; |
24 |
[self.window makeKeyAndVisible]; |
TestViewController.h 注意这里面引入了很多代理类。
01 |
#import <UIKit/UIKit.h> |
03 |
@interface TestViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> |
06 |
UITextView *_textEditor; |
09 |
UIActionSheet *myActionSheet; |
TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。
001 |
#import "TestViewController.h" |
003 |
@interface TestViewController () |
007 |
@implementation TestViewController |
013 |
self.navigationItem.title = @ "雨松MOMO输入框" ; |
016 |
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] |
018 |
style: UIBarButtonItemStyleDone |
020 |
action: @selector(sendInfo)] autorelease]; |
023 |
_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; |
025 |
_textEditor.delegate = self; |
026 |
_textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth; |
027 |
_textEditor.keyboardType = UIKeyboardTypeDefault; |
028 |
_textEditor.font = [UIFont systemFontOfSize:20]; |
029 |
_textEditor.text = @ "请输入内容" ; |
032 |
//这里表示进入当前ViewController直接打开软键盘 |
033 |
[_textEditor becomeFirstResponder]; |
036 |
[self.view addSubview:_textEditor]; |
038 |
//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册 |
039 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@ "camera" ofType:@ "png" ]]; |
041 |
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; |
042 |
button.frame = CGRectMake(0, 120, image.size.width, image.size.height); |
044 |
[button setImage:image forState:UIControlStateNormal]; |
046 |
[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside]; |
049 |
[self.view addSubview:button]; |
056 |
myActionSheet = [[UIActionSheet alloc] |
059 |
cancelButtonTitle:@ "取消" |
060 |
destructiveButtonTitle:nil |
061 |
otherButtonTitles: @ "打开照相机" , @ "从手机相册获取" ,nil]; |
063 |
[myActionSheet showInView:self.view]; |
064 |
[myActionSheet release]; |
068 |
- ( void )actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex |
072 |
if (buttonIndex == myActionSheet.cancelButtonIndex) |
092 |
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; |
093 |
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) |
095 |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; |
096 |
picker.delegate = self; |
098 |
picker.allowsEditing = YES; |
099 |
picker.sourceType = sourceType; |
101 |
[self presentModalViewController:picker animated:YES]; |
104 |
NSLog(@ "模拟其中无法打开照相机,请在真机中使用" ); |
111 |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; |
113 |
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; |
114 |
picker.delegate = self; |
116 |
picker.allowsEditing = YES; |
117 |
[self presentModalViewController:picker animated:YES]; |
122 |
-( void )imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
126 |
NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; |
129 |
if ([type isEqualToString:@ "public.image" ]) |
132 |
UIImage* image = [info objectForKey:@ "UIImagePickerControllerOriginalImage" ]; |
134 |
if (UIImagePNGRepresentation(image) == nil) |
136 |
data = UIImageJPEGRepresentation(image, 1.0); |
140 |
data = UIImagePNGRepresentation(image); |
144 |
//这里将图片放在沙盒的documents文件夹中 |
145 |
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@ "Documents" ]; |
148 |
NSFileManager *fileManager = [NSFileManager defaultManager]; |
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]; |
155 |
filePath = [[NSString alloc]initWithFormat:@ "%@%@" ,DocumentsPath, @ "/image.png" ]; |
158 |
[picker dismissModalViewControllerAnimated:YES]; |
162 |
UIImageView *smallimage = [[[UIImageView alloc] initWithFrame: |
163 |
CGRectMake(50, 120, 40, 40)] autorelease]; |
165 |
smallimage.image = image; |
167 |
[self.view addSubview:smallimage]; |
173 |
- ( void )imagePickerControllerDidCancel:(UIImagePickerController *)picker |
176 |
[picker dismissModalViewControllerAnimated:YES]; |
181 |
NSLog(@ "图片的路径是:%@" , filePath); |
183 |
NSLog(@ "您输入框中的内容是:%@" , _textEditor.text); |
186 |
- ( void )viewDidUnload |
188 |
[super viewDidUnload]; |
189 |
// Release any retained subviews of the main view. |
192 |
- ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
194 |
return (interfaceOrientation == UIInterfaceOrientationPortrait); |
如下图所示,打开下拉菜单按钮开始选择打开相机 或者 打开本地相册。模拟器中是无法打开照相机的的,切记。

如下图所示,这里就是我本地的相册啦,里面保存了几张图片,选择一张即可。

我在这里再说说图片上传, 图片上传我们采用的是2进制ASIHTTPRequest 来完成的。
发送请求
01 |
NSString *server_base = [NSString stringWithFormat:@ "%@/users/uploadResource.json" , _server]; |
03 |
ASINetworkQueue *queue = [[ASINetworkQueue alloc] init]; |
05 |
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:server_base]]; |
07 |
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: NO]; |
08 |
[request setDelegate :self]; |
09 |
[request setDidFinishSelector:@selector(sendCommentSucc:)]; |
10 |
[request setDidFailSelector:@selector(sendCommentFail:)]; |
12 |
[request setFile:res forKey:@ "res" ]; |
14 |
[queue addOperation:request]; |
最后是文本的源码下载地址:http://vdisk.weibo.com/s/accm9
- IOS研究院之打开照相机与本地相册选择图片(六)
原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六) Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈. 这篇文章主要学习 ...
- 微信小程序:从本地相册选择图片或使用相机拍照。
wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 示例代码: wx.chooseImage({ count: 1, // 默认9 sizeTyp ...
- android 开启本地相册选择图片并返回显示
.java package com.jerry.crop; import java.io.File; import android.app.Activity; import android.conte ...
- 微信小程序chooseImage(从本地相册选择图片或使用相机拍照)
一.使用API wx.chooseImage(OBJECT) var util = require('../../utils/util.js') Page({ data:{ src:"../ ...
- HTML5 Plus 拍照或者相册选择图片上传
HBuilder+HTML5 Plus+MUI实现拍照或者相册选择图片上传,利用HTML5 Plus的Camera.Gallery.IO.Storage和Uploader来实现手机APP拍照或者从相册 ...
- [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现
[Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...
- 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传
一.Flutter image_picker 实现相机拍照和相册选择 https://pub.dev/packages/image_picker 二.Flutter 上传图片到服务器 ht ...
- ng-cordova 手机拍照或从相册选择图片
1.需求描述 实现一个调用摄像头拍照,或者直接打开本地图库选择照片,然后替换App中图片的功能 2.准备 1) 安装ng-cordova 进入到ionic工程目录,使用bower工具安装, bower ...
- 浅谈Android中拍照、从相册选择图片并截图相关知识点
前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...
随机推荐
- Shader的使用
一.LinearGradient 步骤:①.创建LinearGradient 步骤 ②.将其加入到Paint 步骤一: 构造LinearGradient的参数 public LinearGrad ...
- Office OpenXML-Excel(一)
原文 http://www.cnblogs.com/changminglong/articles/2840004.html 适用于 2007 Microsoft Office 套件,Microsoft ...
- Spring再学习
一.主要版本变更 框架最早发布于2004年,其后发布了几个重大的版本更新:在Spring 2.0中提供对XML命名空间和AspectJ的支持:Spring 2.5中新增了注解驱动的配置:在Spring ...
- stl_map,set 用法
set: 集合a,b加起来,去重 hdu 1406 #include <iostream> #include<cstdio> #include<set> using ...
- UML_静态图
类图 类图是描述类,接口,协作以及它们之间关系的图,用来显示系统中各个类的静态结构.类图是定义其他图的基础,在类图的基础上,可以使用状态图,协作图,组件图和配置图等进一步描述系统其他方面 ...
- 轻量级交互数据json格式初探
[w3cschool tydef]什么是 JSON ?JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)JSON 是轻量级的文本数据交换格式JS ...
- js原生 + jQuery实现页面滚动字幕
js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 ...
- 黑马程序员_<<Set,HashSet>>
--------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1.Set Set是Collection接口 ...
- 深入浅出CChart 每日一课——第十六课 实习之旅,百年老店之新锐WTL
上节课笨笨给大家介绍了CChart在微软MFC框架下的应用,本节课的内容仍然和百年老店微软相关,只不过主角换成WTL了. 不了解WTL的同学可以先找度娘温习一下.度娘在怀,今生何求.郎君啊,你是不是闷 ...
- windows+Ubuntu双系统 windows引导修复
我的博客:http://blog.csdn.net/muyang_ren 装完windows+Ubuntu麒麟双系统后,发现引导是Ubuntu的. Ubuntu的引导是GRUP windows的引导是 ...