0

I need to upload the Image into the web server in stream format. Here i used Image Picker to select the images from the gallery.

3 Answers3

0
- (IBAction)selectingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ALERT_TITLE" 
                                                        message:@"ALERT_MSG" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}

#pragma mark -

   - (void) imagePickerController:(UIImagePickerController *)picker
         didFinishPickingImage:(UIImage *)image
         editingInfo:(NSDictionary *)editingInfo
{
   imageData= UIImagePNGRepresentation(image);   ////declare as a public variable in iterface
 [picker dismissModalViewControllerAnimated:YES];
}




- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

-(void)WebserviceCallMtd
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Martin" forKey:@"names"];
[request addPostValue:@"Newyork" forKey:@"City"];
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];
}
Bhoomi Jagani
  • 2,413
  • 18
  • 24
0
// add image data
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);


NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • for more information use this http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post, http://stackoverflow.com/questions/18442672/send-image-to-net-web-service – Anbu.Karthik Jun 07 '14 at 10:32
  • i tried it karthik but the error is connection timed out. here the tragedy is in parallel with image i send textfields input. those inputs are saving in database . for the image its showing null –  Jun 07 '14 at 11:52
  • i sent a mail to u. check it –  Jun 07 '14 at 12:00
0

elow is my code , i am using in my project

- (void)uploadFile:(NSData*)imgData FileName:(NSString*)fileName Path:(NSString*)postPath{

isFileUploading = YES;
if (appDel.isOnline)
{
    NSLog(@"file path = %@",postPath);

    NSData *_lastImageData = [NSData dataWithData:imgData];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postPath]];

    [request setHTTPMethod:@"POST"];

    [request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];

    [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[_lastImageData length]] forHTTPHeaderField:@"Content-Length"];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request] ;

    operation.inputStream =  [NSInputStream inputStreamWithData:_lastImageData];


    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        if (totalBytesExpectedToWrite > 0) {

            float progress = totalBytesWritten*100/totalBytesExpectedToWrite;

            NSLog(@"%f",progress);

        }
    }];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


        NSLog(@"Sent image: ");
        [self.delegate uploadCompletedWithStatus:YES];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"Failed to send image");

        NSLog(@"Error sending:  Code:%li",(long)[error code]);



    }];


    [operation start];

}
else
{

}   

}

Waseem Sarwar
  • 2,645
  • 1
  • 21
  • 18