Monday 29 August 2011

Getting images and uploading it on webServer


This Blog entry describes how to open photo gallery for selecting images and uploading them on server.

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

If you want image to edit the image then write
imagePicker.allowsEditing = YES;

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];

There are two methods of UIImagePickerController and this method is called when the image is selected.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.image=nil;
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.image)];
NSString *base64string=[imagedata base64EncodedString];
NSString *str = [NSString stringWithFormat:testurl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:base64string forKey:@"keyForUploadingImage"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(SuccessResponse:)];
[request setDidFailSelector:@selector(SuccessresponseFailed:)];
[networkQueue addOperation: request];
[networkQueue go];
[self dismissModalViewControllerAnimated:YES];
}

If u want to upload the edited image instead of original image then write:-
image = [info objectForKey:UIImagePickerControllerEditedImage];

Setting the selected image of phone in the imageView

[self.ImageView setImage:image];

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

No comments:

Post a Comment