前几天研究了下nsurlsession,现在分享下上传图片的代码
NSURL *url = [NSURL URLWithString:@"http://192.168.23.1/Fanfou/php/uploadPicForIOS.php"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
NSError *error = nil;
NSData *data =UIImageJPEGRepresentation([UIImage imageNamed:@"pic.jpg"], 1.0);
if (!error) {
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
if(error){
NSLog(@"%@",error);
}else{
NSLog(@"%@",respon