Capture and Upload Image on server iOS objective c






ImageSelectCapture.h

#import <UIKit/UIKit.h>

@interface ImageSelectCapture : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *img_show_;

- (IBAction)btSelectImage:(UIButton *)sender; // button to select image
- (IBAction)btUpload:(UIButton *)sender;  // button to upload image on server

@end


ImageSelectCapture.m


#import "ImageSelectCapture.h"

@interface ImageSelectCapture ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    
    
    NSData *pngData; //store image data when selected
    NSString *image_name;
}

@end

@implementation ImageSelectCapture

- (void)viewDidLoad {
    [super viewDidLoad];
    
pngData = nil;
   image_name=@"filename.jpg";
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
Called on user select image
*/

- (IBAction)btSelectImage:(UIButton *)sender {
    
    UIImagePickerController *pickerControl=[[UIImagePickerController alloc]init];
    
    pickerControl.delegate=self;
    pickerControl.allowsEditing=YES;
    
    pickerControl.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    
/**
If want to capture from camera
**/
//pickerControl.sourceType=UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:pickerControl animated:YES completion:NULL];

    
}

/**
 Upload Button Click
 **/
- (IBAction)btUpload:(UIButton *)sender {
    
    NSString *img_upload_url=@“filename.php";
    
    
    if(pngData!=nil){
        
        NSMutableURLRequest *request;
        request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:img_upload_url]];
        
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setHTTPShouldHandleCookies:NO];
        [request setTimeoutInterval:60];
        [request setHTTPMethod:@"POST"];
        
        NSString *boundary = @"unique-consistent-string";
        
        // set Content-Type in HTTP header
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
        [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
        
        // post body
        NSMutableData *body = [NSMutableData data];
        
        // add params (all params are strings)
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"imageCaption"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
        
        
        
        
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"image",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:pngData];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
        // setting the body of the post to the reqeust
        [request setHTTPBody:body];
        
        // set the content-length
        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            if(data.length > 0)
            {
                NSLog(@"Successfully Uploaded");
            }
        }];
        
    }
}

/**
 select Image and load with data
*/

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *chosen_image=info[UIImagePickerControllerEditedImage];
    
    _img_show_.image=chosen_image;
    
    /**
     Store image on data
    **/
    
    pngData = UIImageJPEGRepresentation(chosen_image,0.1);
    
    [picker dismissViewControllerAnimated:YES completion:NULL];

}

/**
 If user cancel the operation
 */
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{

    [picker dismissViewControllerAnimated:YES completion:NULL];
}


@end

Comments