如下圖所示 在本地相冊中選擇一張圖檔後,我們将他拷貝至沙盒當中,在用戶端中将它的縮略圖放在按鈕旁邊,這個結構其實和新浪微薄中選擇圖檔後的效果一樣。最終點選發送将按鈕将圖檔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 |
009 | - ( void )viewDidLoad |
013 | self.navigationItem.title = @ "雨松MOMO輸入框" ; |
016 | self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] |
017 | initWithTitle: @ "發送" |
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 |
175 | NSLog(@ "您取消了選擇圖檔" ); |
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