Search code examples
objective-cimageuiimagepickercontrollerafnetworking

How can I upload an image file from saved NSUserDefault to php server?


I would like to change profile image in my apps and upload the image to PHP server after edited. So far I saved my changed profile image to local.

My question is how can I upload the changed image to PHP server (upload image file)? Can I use AFNetworking 3.X and I need to write backend API for this case? Is there any method to upload the image file to server directly?

Here is what I done so far by using UIImagePickerController and it is working fine until this stage:-

-(void)changeProfileImage:(UITapGestureRecognizer *)recognizer{

    if (!_photoManager) {
        _photoManager =[[SelectPhotoManager alloc]init];
    }
    [_photoManager startSelectPhotoWithImageName:@"Select Profile Image"];
    __weak typeof(self)mySelf=self;
    //Select Photo
    _photoManager.successHandle=^(SelectPhotoManager *manager,UIImage *image){

        mySelf.imgProfileImage.image = image;
        NSData *data = UIImagePNGRepresentation(image);
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"imgProfile"];
    };
}

Please help. Thank you.


Solution

  •     // HTTP method to upload file to web server
    -(void)initWithURL:(NSString *)url image:(NSData *)imageData andFileName:(NSString *)filename delegate:(id)del {
        // your PHP server url
        // your image data
        NSString *urlString = url;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];
    
        // if access token defined for authrization
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[[[CommonUserDefault getUserInfo] objectForKey:@"token"] objectForKey:@"accessToken"]];
        [request addValue:token forHTTPHeaderField: @"Authorization"];
    
        NSMutableData *body = [NSMutableData data];
    
        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@.jpg\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
            // close form
        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            // set request body
        [request setHTTPBody:body];
    
            //return and test
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
        id json = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
         NSLog(@"json upload picture = %@",json);
    
        int statusCode = [[json objectForKey:@"statusCode"] intValue];
        if(statusCode == <errorCode>){
        }
    }