将 UIImage 发布到网络上
我刚刚开始从事 xcode 业务,到目前为止一切顺利,除了这个小细节:我不知道如何将 UIImage 发布到网络。在过去的三天里我一直在尝试一切(阅读和查询了很多)但没有成功。我想答案就在我眼皮子底下,仍然……不走运。
我把我写的代码(在 Objective-C 和 php 中)留在这里:它根本不起作用!没有上传图像。请问有人可以帮助我吗?
//objective-C
NSData *theData = UIImagePNGRepresentation(photo.image);
NSString *urlString = @"http://www.testes01.com/newsie/receberprofilephoto.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"iphoneimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//PHP
move_uploaded_file($_FILES["file"][tmp_name], "fotosprofile/" . $_FILES["file"]["name"]);
提前致谢。
I'm just starting with the xcode business and so far so good, except for this tiny detail: I don't know how to post a UIImage to the web. I've been trying everything for the last 3 days (read and queried a lot) without success. The answer, I think is under my nose, still... no luck.
I leave here the code I wrote (either in objective-c and php): it doesn't work at all!! No image is uploaded. Please, could someone help me?
//objective-C
NSData *theData = UIImagePNGRepresentation(photo.image);
NSString *urlString = @"http://www.testes01.com/newsie/receberprofilephoto.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"file\"; filename=\"iphoneimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//PHP
move_uploaded_file($_FILES["file"][tmp_name], "fotosprofile/" . $_FILES["file"]["name"]);
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是在网络上上传图像的代码
self.image 是我的 uiimage 对象,我已将其设置为属性并合成它。然后将您的图像放入 self.image 中:-
我希望这可以帮助您!!!!
Here is the code for image uploading on a web
self.image is my uiimage object which i have made property and synthesize it.Get your image in self.image then:-
I hope this might help you!!!!