问题
I have to upload an image taken via the camera or uploaded via uiimagepickerview
. I am using ASIHTTPRequest.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
1.) Since i am taking the photo from the iPhone camera or uploading it from the uiimagepickerview
, i need to know what value should i set for setFile
(replacement text for @"/Users/ben/Desktop/ben.jpg"
) ?
2.) I also need to test this application, so does anyone know the corresponding PHP code where i could display the image taken (on the web browser). Tutorials or sample codes
回答1:
To get answer on the 1st question, please, take a look on the following code fragment:
#import <MobileCoreServices/MobileCoreServices.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
BOOL isImage = ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:kUTTypeImage]);
if (isImage) {
self.request = [ASIFormDataRequest requestWithURL:url];
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(originalImage, 0.7);
[self.request setData:imageData
withFileName:@"photo.jpg"
andContentType:@"image/jpeg"
forKey:@"photo"];
[self.request start];
}
}
Please consider to add property into your UIImagePiclerController delegate class
@property(nonatomic, retain) ASIFormDataRequest *request;
It will allow you to stop request or at least set it's delegate to nil, if your object is deallocated, by simply implementing
- (void) dealloc {
[self.request cancel];
self.request.delegate = nil;
[request release];
[super dealloc];
}
回答2:
You should Add Image to the Request Body as image data.
Take a Look of ASIHttp Framework
And Even you don't get Please Go through this Link
来源:https://stackoverflow.com/questions/9555101/uploading-image-to-the-web-service-beginner