将 UIImage 发布到网络上

发布于 2024-10-22 11:21:03 字数 1909 浏览 3 评论 0原文

我刚刚开始从事 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-10-29 11:21:03

这是在网络上上传图像的代码
self.image 是我的 uiimage 对象,我已将其设置为属性并合成它。然后将您的图像放入 self.image 中:-

NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.image)];
    NSString *base64string=[imagedata base64EncodedString];
    NSString *str = [NSString stringWithFormat:@"http://www.testes01.com/newsie/receberprofilephoto.php"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:base64string forKey:@"imagedata"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseStatusString %@",[request responseString]);

我希望这可以帮助您!!!!

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:-

NSData *imagedata=[NSData dataWithData:UIImagePNGRepresentation(self.image)];
    NSString *base64string=[imagedata base64EncodedString];
    NSString *str = [NSString stringWithFormat:@"http://www.testes01.com/newsie/receberprofilephoto.php"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:base64string forKey:@"imagedata"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseStatusString %@",[request responseString]);

I hope this might help you!!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文