ios - 从 iOS 上传图片到 PHP 服务器

标签 ios objective-c image web-services

我知道之前也有人问过这个问题,但我的问题有点不同。

我想将图像上传到 PHP 服务器,并且我想从 iOS 发送更多参数和图像。 我在谷歌上搜索,找到了两种解决方案:

  1. 要么我们将以 JSON 格式的 Base64 编码字符串形式发送图像。转介link .

  2. 或者我们将使用表单数据将图像上传到服务器。我已经提到了这个 link .如果有人这样推荐我,请帮我在这个 API 中添加更多参数。

现在我的问题是,哪种方式是将图像上传到服务器的最佳方式,我必须在同一个 Web 服务调用中发送更多参数(用户名、密码和更多详细信息)。

提前致谢。

最佳答案

您可以通过以下两种方式将图像从 iOS 应用程序上传到 PHP 服务器:

使用 New AFNetworking :

#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"

    NSString *stringUrl =@"http://www.myserverurl.com/file/uloaddetails.php?"
    NSString *string =@"http://myimageurkstrn.com/img/myimage.png"       
    NSURL *filePath = [NSURL fileURLWithPath:string];

   NSDictionary *parameters  = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         [formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image 
     }
     success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"%@",[responseObject valueForKey:@"Root"]);
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];     

     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
         [Alert_Success_fail show];

     }];

第二次使用NSURLConnection:

-(void)uploadImage
    {       
        NSData *imageData = UIImagePNGRepresentation(yourImage);

        NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1];

        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:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

        [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    }

这两种方式都可以很好地将图像从应用程序上传到 php 服务器,希望对您有所帮助。

关于ios - 从 iOS 上传图片到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728317/

相关文章:

ios - 将数据从 VC 发送到单元,无需转换

ios - Postman 给出成功响应,而通过代码调用相同的 API 时抛出错误

ios - UITabBar如何设置圆角和阴影?

objective-c - cocoa 分布式对象

ios - NSNotificationCenter 使我的应用程序崩溃

ios - 在 iOS 今天小部件中从 url 加载图像时出现内存错误

image - 如何使用其内容识别图像文件格式?

ios - 如何使用选项制作 switch 语句?

iphone - 在 UIPageViewController 的底部放置一个栏

java - Android 位图缩小