上傳文件至服務器
來源:程序員人生 發布時間: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個頭文件
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈