我正在尝试上传使用UIImagePickerController选择的图片。 UIImagePickerController给出了我可以在视图中显示的UIImage对象,但它没有给出路径(可能是它,我看错了地方)。
我正在使用ASIFormDataRequest来上传内容,但这需要上传图片的路径。
有什么建议吗?
谢谢
答案 0 :(得分:4)
如果您拥有UIImage,而不需要本地路径上传图片,可以使用以下代码上传。
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.
}
如有任何疑问,请发表评论。
希望它有所帮助。