上传使用 UIImagePickerController 选择的图片 [英] Upload picture selected using UIImagePickerController

查看:23
本文介绍了上传使用 UIImagePickerController 选择的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传使用 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 选择的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆