
//Create a folder named images in your server where you want to upload the image.// And Create a PHP file and use below code .<?PHP$uploaddir = 'images/';$ran = rand () ;$file = basename($_fileS['userfile']['name']);$uploadfile = $uploaddir .$ran.$file;if (move_uploaded_file($_fileS['userfile']['tmp_name'],$uploadfile)) { echo "www.host.com/.../images/{$uploadfile}";}?> 这是iOS代码
- (IBAction)uploadClicked:(ID)sender{ /* turning the image into a NSData object getting the image back out of the UIImageVIEw setting the quality to 90 */ NSData *imageData = UIImageJPEGRepresentation(imageVIEw.image,90); // setting up the URL to post to Nsstring *urlString = @"your URL link"; // setting up the request object Now NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request sethttpMethod:@"POST"]; /* add some header info Now we always need a boundary when we post a file also we need to set the content type You might want to generate a random boundary.. this is just the same as my output from wireshark on a valID HTML post */ Nsstring *boundary = [Nsstring stringWithString:@"---------------------------14737809831466499882746641449"]; Nsstring *ContentType = [Nsstring stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:ContentType forhttpheaderFIEld: @"Content-Type"]; /* Now lets create the body of the post */ NSMutableData *body = [NSMutableData data]; [body appendData:[[Nsstring stringWithFormat:@"rn--%@rn",boundary] dataUsingEnCoding:NSUTF8StringEnCoding]]; [body appendData:[[Nsstring stringWithString:@"Content-disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEnCoding:NSUTF8StringEnCoding]]; [body appendData:[[Nsstring stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEnCoding:NSUTF8StringEnCoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[Nsstring stringWithFormat:@"rn--%@--rn",boundary] dataUsingEnCoding:NSUTF8StringEnCoding]]; // setting the body of the post to the reqeust [request sethttpBody:body]; // Now lets make the connection to the web NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; Nsstring *returnString = [[Nsstring alloc] initWithData:returnData enCoding:NSUTF8StringEnCoding]; NSLog(returnString);} 总结 以上是内存溢出为你收集整理的iPhone – 如何将图像发布到Web服务器全部内容,希望文章能够帮你解决iPhone – 如何将图像发布到Web服务器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)