Image uploaded from iPhone gets corrupted
I'm trying to take a photo from my iPhone app and upload to a WCF REST service.
I'use the UIImagePickerController to access t开发者_Go百科he camera and assign the new photo to a UIImageView on my view.
Then I use the UIImageJPEGRepresentation to convert it to "real" data.
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 0.2);
Then I try submitting a request to my service like this:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setFile:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"imageData"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request startAsynchronous];
The service receives the data and writes it to the disk on my webserver. But I can't open the image on the server, as Windows tells me the file was corrupted. I'm guessing I need so set some kind of encoding or content type in my iPhone app. I do receive HTTP status code 200 from my service after the upload is through.
I've made a small C# client app just to test the service, and when I upload a photo from my Windows machine using this client everything is fine. So I guess it has to been something I'm missing in y request from the iPhone.
Thanks for reading, and for any help you can provide.
After trying a ton of approaches I finally got it working using:
[request appendPostData:imageData];
instead of:
[request setFile:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"imageData"];
精彩评论