多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 上傳文件至服務器

上傳文件至服務器

來源:程序員人生   發布時間:2014-12-13 08:47:52 閱讀次數:3226次
//上傳圖片 -(void)showActionSheet { //在這里呼出下方菜單按鈕項 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 presentViewController:picker animated:YES completion:nil]; }else { NSLog(@"摹擬其中沒法打開照相機,請在真機中使用"); } } //打開本地相冊 -(void)LocalPhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //設置選擇后的圖片可被編輯 picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil]; [picker release]; } //當選擇1張圖片落后入這里 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //當選擇的類型是圖片 if ([type isEqualToString:@"public.image"]) { NSLog(@"您選擇了該圖片"); //先把圖片轉成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 dismissViewControllerAnimated:YES completion:nil]; [self imageUpload:image]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"您取消了選擇圖片"); [picker dismissViewControllerAnimated:YES completion:nil]; } //上傳圖片到服務器方法,采取了ASIFormDataRequest方法 - (void) imageUpload:(UIImage *) image{ UIImage *im = [UIImage imageWithContentsOfFile:filePath];//通過path圖片路徑獲得圖片 NSData *data = UIImagePNGRepresentation(im);//獲得圖片數據 /* ios中獲得圖片的方法有兩種,1種是UIImageJPEGRepresentation ,1種是UIImagePNGRepresentation 前者獲得到圖片的數據量要比后者的小很多。。 */ //服務器地址 NSURL *url = [NSURL URLWithString:@"這里寫入你的服務器上傳地址"]; aRequest = [[ASIFormDataRequest alloc] initWithURL:url]; [aRequest setDelegate:self];//代理 [aRequest setRequestMethod:@"POST"]; //這個參數還不是很清楚 [aRequest addData:data withFileName:@"test.png" andContentType:@"image/png" forKey:@"file"]; [aRequest addRequestHeader:@"Content-Type" value:@"multipart/form-data"];//這里的value值 需與服務器端 1致 [aRequest startAsynchronous];//開始。異步 [aRequest setDidFinishSelector:@selector(headPortraitSuccess)];//當做功后會自動觸發 headPortraitSuccess 方法 [aRequest setDidFailSelector:@selector(headPortraitFail)];//如果失敗會 自動觸發 headPortraitFail 方法 } -(void)headPortraitSuccess{ NSData * myResponseData = [aRequest responseData]; //獲得數據 : 調試的時候,在這里, myResponseData的值1直是nil,不知道為何? //將接收的數據轉換成NSString類型并打印出來 NSString *result = [[NSString alloc] initWithData:myResponseData encoding:NSUTF8StringEncoding]; NSLog(@"result------------>%@", result); NSDictionary *data = [result objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode]; NSArray *imgArray = [data objectForKey:@"data"]; NSString *imgpath=@"http://twww.51qed.com"; if ([imgArray count]) { NSDictionary *imgInfo = [imgArray objectAtIndex:0]; imgpath = [imgpath stringByAppendingString:[imgInfo objectForKey:@"img"]]; NSLog(@"imgpath------------>%@", imgpath); } //設置H5圖片更新,ios調用JS代碼 [webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setNewImg('%@');",imgpath]]; NSLog(@"上傳成功!"); [aRequest release]; } -(void)headPortraitFail{ NSLog(@"上傳失敗!"); } //開始request要求 - (void)requestStarted:(ASIHTTPRequest *)request{ NSLog(@"開始要求!"); } @end
需導入
<pre name="code" class="objc">#import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" #import "JSONKit.h"

這3個頭文件


     
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 可以看黄色的网址 | 国产suv精品一区二区四区三区 | 欧美18videosex性欧美群 | 欧美精品v国产精品v日韩精品 | 91精品国产露脸在线 | 国产日韩欧美亚洲综合 | 性欧美性free| 日本高清中文字幕一区二区三区a | 国产亚洲精品国看不卡 | 一级爱爱片 | 亚洲精品日韩中文字幕久久久 | 免费国产成高清人在线视频 | 91精品国产综合久久久久久 | 一区二区三区四区在线播放 | 亚洲14p| 视频网站高清免费 | 午夜影放免费观看 | 秋霞理论在一l级毛片 | 亚洲国产欧美目韩成人综合 | 欧美一级大黄特黄毛片视频 | 日一区二区三区 | 一级毛片视频免费 | 一级特黄aa大片一又好看 | 18在线观看国内精品视频 | 国内精品免费视频 | 成人性毛片 | 一区二区三区四区在线免费观看 | 成人在线亚洲 | 亚洲性生活视频 | 樱花aⅴ一区二区三区四区 影视精品网站入口 | 68久久久久欧美精品观看 | 亚洲欧美自拍另类图片色 | 日韩拍拍拍| 免费观看老外特级毛片 | www.av在线播放 | 在线美女免费观看网站h | 欧美三级中文字幕hd | 乱在线伦视频免费 | 欧美一区二区三区不卡免费 | 美女的隐私视频网站蜜桃视频 | 国产精品国产三级在线高清观看 |