iOS NSMutableURLRequest 上傳圖片

來源:互聯網
上載者:User

標籤:

- (void)postImage:(UIImage *)_image{    //分界線的標識符    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";    //根據url初始化request    NSInteger schoolID = [[BaseUser shareInstance].userIdentity.school_id integerValue];    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"url"]                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData                                                       timeoutInterval:10];    //分界線 --AaB03x    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];    //結束符 AaB03x--    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];    //要上傳的圖片    UIImage *image= _image;    //得到圖片的data    NSData* data = UIImagePNGRepresentation(image);    //http body的字串    NSMutableString *body=[[NSMutableString alloc]init];    //參數的集合的所有key的集合    NSDictionary *param = @{};    NSArray *keys = [[param keyValues] allKeys];        //遍曆keys    for(int i=0;i<[keys count];i++)    {        //得到當前key        NSString *key=[keys objectAtIndex:i];        //如果key不是pic,說明value是字元類型,比如name:Boris        if(![key isEqualToString:@"pic"])        {            //添加分界線,換行            [body appendFormat:@"%@\r\n",MPboundary];            //添加欄位名稱,換2行            [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];            //添加欄位的值            [body appendFormat:@"%@\r\n",[[param keyValues] objectForKey:key]];        }    }        ////添加分界線,換行    [body appendFormat:@"%@\r\n",MPboundary];    //聲明pic欄位,檔案名稱為boris.png    [body appendFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"boris.png\"\r\n"];    //聲明上傳檔案的格式    [body appendFormat:@"Content-Type: image/png\r\n\r\n"];        //聲明結束符:--AaB03x--    NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];    //聲明myRequestData,用來放入http body    NSMutableData *myRequestData=[NSMutableData data];    //將body字串轉化為UTF8格式的二進位    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];    //將image的data加入    [myRequestData appendData:data];    //加入結束符--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:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];    //設定http body    [request setHTTPBody:myRequestData];    //http method    [request setHTTPMethod:@"POST"];        //建立串連,設定代理    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        if (connectionError) {                               }else{             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];            NSLog(@"上傳成功");        }    }];}

 

iOS NSMutableURLRequest 上傳圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.