在IOS设备上将图像上传到服务器

时间:2014-10-23 23:36:53

标签: ios objective-c

我正在使用NSMutableURlRequest将图片发送到我的服务器。我能够在服务器端读取request.body.image,但是当我尝试打开图片时,我无法做到这一点(我通过将其从我的服务器上传到amazon s3并将其公开)来打开它。这是Obj C代码:

- (bool)sendProlilePic: (UIImage*)image {
    //We need the email and password for this request
    NSString *password = [self.keychainItem objectForKey:(__bridge id)(kSecValueData)];
    NSString *email= [self.keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];

    // Create the request.
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/set_profilepic"]];
    // Specify that it will be a POST request
    request.HTTPMethod = @"POST";

    // This is how we set header fields
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    //make a mutable data to store all the information
    NSMutableData *requestBodyData = [NSMutableData data];


    // add loggin information and image to requestBodyData
    NSString *stringData = [NSString stringWithFormat:@"email=%@&password=%@&image=",email,password];
    [requestBodyData appendData:[stringData dataUsingEncoding:NSUTF8StringEncoding]];
   // NSData *imageData = UIImagePNGRepresentation(image);//convert the image
    [requestBodyData appendData:[NSData dataWithData: UIImageJPEGRepresentation(image, 1.0)]];//append the image
    request.HTTPBody = requestBodyData;

    // Create url connection and fire request
    NSURLResponse * response = nil;//the raw response will be stored in this variable
    NSError * error = nil;//if an error occurs then it will be stored in this variable
    //send an http post request to the server
    NSData * data = [NSURLConnection sendSynchronousRequest:request
                                      returningResponse:&response
                                                  error:&error];
    //convert the server response into a string
    NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //parse the HTTP response and extract the statusCode
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (error == nil)//if there is no error (server is reachable)
    {
        if(statusCode == 200){
            if(error){
                [self displayAlertWithTitle:@"Error" Content:@"Looks like we did something stupid and we're trying our best to fix it. Thank you for your patience"];
                return false;
            }else{
                return true;//if the profile picture has been uploaded, return true. Otherwise return false
            }
        }else if(statusCode == 403){
            //user is doing something wrong
            //create a UIview controller and display the error message
            [self displayAlertWithTitle:@"Error" Content:stringFromData];
            return false;

        }else{
            //something is wrong and you don't know what it is
            //create a UIview controller and display a generic error message
            [self displayAlertWithTitle:@"Error" Content:@"Server is not responding. Please try again later"];
            return false;
        }
    }else{
        //either the server is down or user's internet connection is down
        [self displayAlertWithTitle:@"Error" Content:@"Please check your internet connection. We might be updating our servers. Please try again later"];
        return false;
    }}

我对目标C相当新。有人可以检查我是否犯了一个明显的错误吗?

0 个答案:

没有答案