我正在尝试上传使用UIImagePickerController选择的图片。 UIImagePickerController给出了我可以在视图中显示的UIImage对象,但它没有给出路径(可能是它,我看错了地方)。
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).
我正在使用ASIFormDataRequest上传内容,但这需要上传图片的路径。
I'm using ASIFormDataRequest to upload content, but this would need path to the picture to upload it.
有什么建议吗?
谢谢
推荐答案如果您有UIImage,那么您不需要本地路径来上传图片,你可以使用以下代码上传它。
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.
希望有所帮助。
更多推荐
上传使用UIImagePickerController选择的图片
发布评论