upload image from iphone to the server folder
/*
turning the image into a NSData object
getting the image back out of the UIImageView
setting the quality to 90
*/
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
// setting up the URL to post to
NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
this code will be helpful for you....
upload images and texts to server
For uploading images to the server you need the following code:
NSURL *url = [NSURL URLWithString:@"http://www.xyz.com/UploadImage.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = @"POST";
NSString *fileName = @"iphone.jpg";
[request addPostValue:fileName forKey:@"name"];
// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
[request setData:imageData withFileName:fileName andContentType:@"image/jpeg" forKey:@"image"];
[request setDelegate:self];
[request startAsynchronous];
For sending text to the server, you just need to append the text with the POST method like:
[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
Cheers!!!
post image to server in iphone
Giving the Same Answer 2 Time.
How to convert image into binary format in iOS?
You can use the CoreGraphics
' method UIImagePNGRepresentation(UIImage *image)
, which returns NSData
and save it. and if you want to convert it into again UIImage
create it using [UIimage imageWithData:(NSData *data)]
method.
- (void)sendImageToServer {
UIImage *yourImage= [UIImage imageNamed:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
// Init the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imageData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
// response data of the request
}
[request release];
}
iphone SDK : Upload image from iphone to a php Server send empty file ?(sample code link inside)
use HTTP multipart POST or ASIHTTPRequest
@WebberLai
Save the image to home directory:
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
UIImage *img = pickerImage;
// Write a UIImage to JPEG with minimum compression (best quality)
[UIImageJPEGRepresentation(img, 0.5) writeToFile:jpgPath atomically:YES];
NSString *photoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
request = [ASIFormDataRequest requestWithURL:webServiceUrl];
[request setPostValue:requestString forKey:@"data"];
[request setFile:photoPath forKey:@"apiupload"];
[request startSynchronous];
Upload image to PHP web server from iOS
You can use AFNetworking framework (https://github.com/AFNetworking/AFNetworking), it will save a lot of time for you and there are some examples how to upload images and handle responses.
Here is an example of file upload from the source mentioned above:
NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];
Related Topics
App Does Not Have Access to Your Photos or Videos iOS 9
Removing Duplicates from Array of Custom Objects Swift
Xcode - Bundle Format Unrecognized, Invalid, or Unsuitable
Xcode Simulator: How to Remove Older Unneeded Devices
Storyboard View Elements Greyed Out
Swift Version of Componentsseparatedbystring
In Firebase, How to Query the Most Recent 10 Child Nodes
How to Disable Floating Headers in Uitableview with Uitableviewstyleplain
How to Fix Xcode 6.1 Error While Building Ipa
Draw a Circle of 1000M Radius Around Users Location in Mkmapview
How to Color a Uiimage in Swift
How to Check Internet Connection in Alamofire
Upload Image from iPhone to the Server Folder
How to Open Fb and Instagram App by Tapping on Button in Swift
iOS 8 Tab Bar Item Background Colour
Ld: Framework Not Found Parse Xcode 7 Beta
Error: Invalid Bitcode Version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')