How send NSData using POST from a iOS application?

后端 未结 3 882
太阳男子
太阳男子 2020-12-19 10:08
NSData *imageData = UIImagePNGRepresentation(image);

How send imageData using POST?

3条回答
  •  猫巷女王i
    2020-12-19 10:31

    The following code should help

    NSData *imageData = UIImagePNGRepresentation(image);
    
    NSURL *yourURL = ...
    NSMutableURLRequest *yourRequest = [NSMutableURLRequest requestWithURL:yourURL 
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                           timeoutInterval:60.0];
    //Set request to post
    [yourRequest setHTTPMethod:@"POST"];
    
    //Set content type 
    [yourRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
    
    // Set authorization header if required
    ...
    
    // set data
    [yourRequest setHTTPBody:imageData];
    
    
    // create connection and set delegate if needed
    NSURLConnection *yourConnection = [[NSURLConnection alloc] initWithRequest:yourRequest 
                                                                      delegate:self
                                                              startImmediately:YES];
    

    Please note that, it is assumed that you are using ARC.

提交回复
热议问题