如果調(diào)用相機,相冊請查看??這里:
http://www.reibang.com/p/4384efc6fb04
調(diào)用上傳圖片方法
[self postRequestWithURL:@"URL地址" picFileName:@"boris.png" dataimage:@"相冊或相機返回的圖片" andparameters:@"請求的參數(shù)(例如用戶id)" andBlock:^(NSDictionary * _Nonnull retDic, NSError * _Nonnull error) {
if ((YES)) {
//請求成功
}
}];
#pragma makr -- 上傳圖片到服務器
static NSString * const FORM_FLE_INPUT = @"file";//字段名 根據(jù)接口命名
-(void)postRequestWithURL: (NSString *)url picFileName: (NSString *)picFileName
dataimage:(UIImage *)image andparameters:(NSMutableDictionary *) dic
andBlock:(void(^)(NSDictionary *retDic,NSError *error))block
{
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:url] cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
NSData* data;
NSMutableString *body=[[NSMutableString alloc]init];
//參數(shù)的集合的所有key的集合,注意此處,注意此處
NSArray *keys = @[@"file",@"P"];
//遍歷keys
for(int i=0;i<[keys count];i++){
//得到當前key
NSString *key=[keys objectAtIndex:i];
//如果key不是imgfile箕别,說明value是字符類型凳枝,比如name:Boris
if(![key isEqualToString:@"file"])
{
//得到當前key
NSString *key=[keys objectAtIndex:i];
//添加分界線,換行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名稱省古,換2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
//添加字段的值
NSData *datadic=[NSJSONSerialization
dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr=[[NSString alloc]initWithData:datadic encoding:NSUTF8StringEncoding];
[body appendFormat:@"%@\r\n",jsonStr];
}
}
//添加文件
[body appendFormat:@"%@\r\n",MPboundary];
//聲明pic字段,文件名為boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\";
filename=\"%@\"\r\n",FORM_FLE_INPUT,picFileName];
//聲明上傳文件的格式
[body appendFormat:@"Content-Type:image/png\r\n\r\n"];
//聲明結(jié)束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
//聲明myRequestData丧失,用來放入http body
NSMutableData *myRequestData=[NSMutableData data];
//將body字符串轉(zhuǎn)化為UTF8格式的二進制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//將image的data加入
[myRequestData appendData:data];
//加入結(jié)束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//設置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//設置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//設置Content-Length
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//設置http body
[request setHTTPBody:myRequestData];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&connectionError];
block(json,nil);
}else{
block(nil,connectionError);
}
}];
}
這段代碼如有雷同,是我抄襲,我也不知道抄的哪;