For saving Image on Server in iPhone app. first we convert UIImage in NSMutableArray then we convert it into NSData because NSData will store data in bytes format.
For this we use a UIButton, user will click on the button for select Image for saving on to server through post method. Image will be stored in iPad saved phone album.
Code for .h Controller.
@interface UIController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate, UITextFieldDelegate>{
UIPopoverController *popoverController;
UIImagePickerController *imgPicker;
NSMutableArray *arrImage;
}
@property (nonatomic, assign) UIButton *btnImage;
@property (nonatomic, retain) UIPopoverController *popoverController;
@end
Code for .m Controller.
@synthesize btnImage, popoverController,
-(IBAction)btnImage_TouchUpInside:(id)sender{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imgPicker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake(320, 125, 200, 200) inView:btnLogo permittedArrowDirections:UIProgressViewStyleDefault animated:YES];
}
Delegate method for ImagePickerController, used for converting image into NSMutableArray.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[btnLogo setBackgroundImage: [info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
arrImage = [[NSMutableArray alloc]initWithObjects:[info objectForKey:@"UIImagePickerControllerOriginalImage"], nil];
[self.popoverController dismissPopoverAnimated:YES];
[imgPicker release];
}
Post method for saving Image onto Server.
-(void)saveImageToServer{
NSData *dataImage = UIImageJPEGRepresentation([arrImage objectAtIndex:0], 0.8f);
NSString *urlString = @"http://182.71.17.21/sports/index.php?option=com_webservices&task=save_image";
NSString *filename = @"test";
NSMutableURLRequest* request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:dataImage]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString* returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"test %d",returnString);
}
No comments:
Post a Comment