Until now I send strings
to a server with this method:
NSString *reqURL = [NSString stringWithFormat:];
reqURL = [reqURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
[NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error开发者_StackOverflow社区: &err];
and I want to know if there is a possible to send an UIImage
to server.
if your image is png
then use UIImagePNGRepresentation
to get it's data in NSData
.
NSData *data = UIImagePNGRepresentation(myUIImage);
if your image is jpg
or jpeg
then use UIImageJPEGRepresentation
to get it's data in NSData
.
NSData *data = UIImageJPEGRepresentation(myUIImage);
Check in Documentation
Check below SO post for sending NSData
UIImage
to server.
Sending an image data(NSData) to the server
you can try this:-
NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.editedImage)];
NSString *base64string=[imagedata base64EncodedString];
NSString *str = [NSString stringWithFormat:@"%@/uploadBlogData.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:base64string forKey:@"imagedata"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startSynchronous];
NSLog(@"responseStatusCode %i",[request responseStatusCode]);
NSLog(@"responseStatusString %@",[request responseString]);
What this code exactly does it, I have convert my image into NSData and then again encode in base 64 string. You can also do this.
Use ASIHTTPRequest
and do this:
-(void)uploadImage{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"
forKey:@"photo"];
// Upload an NSData instance
[request setData:UIImageJPEGRepresentation(myUIImage) withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[request startAsynchronous];
}
精彩评论