我刚刚开始使用xcode业务,目前为止这么好,除了这个小细节:我不知道如何将UIImage发布到网络。过去3天我一直在尝试一切(阅读和查询很多),但没有成功。答案是,我认为是在我的鼻子下面,仍然...没有运气。在网络上发布UIImage
我离开这里我写的代码(无论是在Objective-C和PHP):它根本不工作!没有图像上传。请,有人可以帮我吗?
//目标-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"]);
预先感谢。
这有效,唯一错误的不是CHMODing文件夹到777,所以你可以读写。 – jcrowson 2012-07-11 09:03:36