Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to upload the picture selected using UIImagePickerController. The UIImagePickerController gives UIImage object that I can display in a view, but it doesn't give a path (may be it does and I'm looking at wrong place).

I'm using ASIFormDataRequest to upload content, but this would need path to the picture to upload it.

Any suggestion ?

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
874 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you have UIImage with you than you don't need local path for image to upload, you can upload it using following code.

NSData *imgData = UIImagePNGRepresentation(YourUIImageObject);

NSURL *url = @"yourURL";

ASIFormDataRequest *currentRequest = [ASIFormDataRequest requestWithURL:url];
[currentRequest setPostFormat:ASIMultipartFormDataPostFormat];
[currentRequest setRequestMethod:@"POST"];
[currentRequest addData:imgData withFileName:@"file" andContentType:@"image/png" forKey:@"yourFileNameOnServer"]; //This would be the file name which is accepting image object on server side e.g. php page accepting file
[currentRequest setDelegate:self];
[currentRequest setDidFinishSelector:@selector(uploadImageFinished:)];
[currentRequest setDidFailSelector:@selector(uploadImageFailed:)];  
[currentRequest startSynchronous];


-(void)uploadImageFinished:(ASIHTTPRequest*)request
{
     //Your request successfully executed. Handle accordingly.
}

-(void)uploadImageFailed:(ASIHTTPRequest*)request
{
     //Your request failed to execute. Handle accordingly.
}

Please leave comment in case of any doubt.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...