下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。
AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。
1
2
3
4
5
6
7
8
9
|
#import <UIKit/UIKit.h>
#import "TestViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;
@property (strong, nonatomic) UIViewController *viewController;
@end
|
AppDelegate.m 在这里就是打开我们创建的TestViewController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navController;
@synthesize viewController;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
self.viewController = [[TestViewController alloc]init];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
@end
|
TestViewController.h 注意这里面引入了很多代理类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
//输入框
UITextView *_textEditor;
//下拉菜单
UIActionSheet *myActionSheet;
//图片2进制路径
NSString* filePath;
}
@end
|
TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#import "TestViewController.h"
@interface TestViewController ()
@end
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//导航栏标题
self.navigationItem.title = @"雨松MOMO输入框";
//导航栏按钮
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle: @"发送"
style: UIBarButtonItemStyleDone
target: self
action: @selector(sendInfo)] autorelease];
//输入框显示区域
_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
//设置它的代理
_textEditor.delegate = self;
_textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_textEditor.keyboardType = UIKeyboardTypeDefault;
_textEditor.font = [UIFont systemFontOfSize:20];
_textEditor.text = @"请输入内容";
//默认软键盘是在触摸区域后才会打开
//这里表示进入当前ViewController直接打开软键盘
[_textEditor becomeFirstResponder];
//把输入框加在视图中
[self.view addSubview:_textEditor];
//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 120, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
//把它也加在视图当中
[self.view addSubview:button];
}
-(void)openMenu
{
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
[myActionSheet showInView:self.view];
[myActionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//呼出的菜单按钮点击后的响应
if (buttonIndex == myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex)
{
case 0: //打开照相机拍照
[self takePhoto];
break;
case 1: //打开本地相册
[self LocalPhoto];
break;
}
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[picker release];
[self presentModalViewController:picker animated:YES];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
[picker release];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
//关闭相册界面
[picker dismissModalViewControllerAnimated:YES];
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
CGRectMake(50, 120, 40, 40)] autorelease];
smallimage.image = image;
//加在视图中
[self.view addSubview:smallimage];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissModalViewControllerAnimated:YES];
}
-(void)sendInfo
{
NSLog(@"图片的路径是:%@", filePath);
NSLog(@"您输入框中的内容是:%@", _textEditor.text);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
|
如下图所示,打开下拉菜单按钮开始选择打开相机 或者 打开本地相册。模拟器中是无法打开照相机的的,切记。
如下图所示,这里就是我本地的相册啦,里面保存了几张图片,选择一张即可。
我在这里再说说图片上传, 图片上传我们采用的是2进制ASIHTTPRequest 来完成的。
发送请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
NSString *server_base = [NSString stringWithFormat:@"%@/users/uploadResource.json", _server];
ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:server_base]];
[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: NO];
[request setDelegate :self];
[request setDidFinishSelector:@selector(sendCommentSucc:)];
[request setDidFailSelector:@selector(sendCommentFail:)];
// res 就是 需要上传图片文件的路径
[request setFile:res forKey:@"res"];
[queue addOperation:request];
[queue go];
|
最后是文本的源码,雨松MOMO祝大家学习愉快,不早了,我也得睡觉啦,1点多了。。。
下载地址:http://vdisk.weibo.com/s/accm9
- 本文固定链接: http://www.xuanyusong.com/archives/1493
- 转载请注明: 雨松MOMO 2012年07月19日 于 雨松MOMO程序研究院 发表