【问题标题】:uploading image picker with AFNetworking使用 AFNetworking 上传图像选择器
【发布时间】:2014-04-23 19:55:04
【问题描述】:

对不起,我的无知。我还不能完全掌握这一切。

所以目前我有代码:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"image.png"]);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL
                                                 URLWithString:@"http://xyz.co.uk/zyx/imageupload.php"]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"image/png"
   forHTTPHeaderField:@"Content-type"];
    [request setValue:[NSString stringWithFormat:@"%lu",
                       (unsigned long)[imageData length]]
   forHTTPHeaderField:@"Content-length"];
     [request setHTTPBody:[self imageDataToSend]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

imageupload.php:

<?php
$handle = fopen("image.png", "wb"); // write binary

fwrite($handle, $HTTP_RAW_POST_DATA);

fclose($handle);

print "Received image file.";
?>

将照片上传到我的网站...完美...不!

我注意到了一些问题。

  1. iphone 顶部没有加载 gif 指示任何类型的活动。
  2. 无法创建委托部门,以便我可以指示是否存在错误或成功。

然后我看向this。这一切都很好,直到我意识到它实际上是垃圾并且我在尝试上传它时收到了一百万个错误。另外每个人都告诉我我走错了方向“ASIHTTPRequest 是旧的。你应该使用 AFNetworking”

我现在正在尝试与 AFNetworking 合作,但事实证明这非常困难!我根本看不出它和 ASIHTTPRequest 有什么相似之处。


谁能帮我用 AFNetwork 做类似的事情。或者完全知道将图像数据上传到我的 php 文件的方法。


来自 Jai Govindani 的错误:

【问题讨论】:

    标签: php ios objective-c afnetworking asihttprequest


    【解决方案1】:

    这是使用 AFNetworking 上传照片的示例 - ZodioAPIClient 是我的 AFHTTPClient 子类。您需要继承 AFHTTPClient (并且通常创建一个单例来执行以下操作)。文件名当然是你的选择,真正重要的是附加 NSData。请注意,这适用于 AFNetworking

    步骤是(这适用于所有 AFNetworking 操作,对照片数据有一些特殊步骤):

    1. 创建一个 NSMutableURLRequest 并附加 NSData
    2. 使用 NSMutableURLRequest 创建一个 AFJSONRequestOperation 并指定您的成功/失败块
    3. 可选 - 指定一个上传进度块,并回调你想用来处理上传进度的任何人/任何人
    4. 将操作入队(如果您不这样做,将不会发生任何事情,操作将不会开始)

    NSData *photo = UIImagePNGRepresentation([UIImage imageNamed:@"image.png"]);
    
    NSMutableURLRequest *addPhotoRequest = [[YourAFHTTPClientSubclass sharedClient] multipartFormRequestWithMethod:@"POST" path:addPhotoPath parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData)
                               {
                                   [formData appendPartWithFileData:photo name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
                               }];
    
    AFJSONRequestOperation *addPhotoRequestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:addPhotoRequest
                                                                                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    
                                                     {
                                                         DDLogVerbose(@"in add photo success block");
                                                     }
                                                      failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                                     {
                                                         DDLogVerbose(@"request that errored: %@", request);
                                                         DDLogVerbose(@"Request failed with error: %@, %@", error, error.userInfo);
                                                         DDLogVerbose(@"the response I got was: %@", JSON);
                                                     }];
    
    [addPhotoRequestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
     {
         DDLogVerbose(@"Total progress: %f", (totalBytesWritten * 1.0f)/(totalBytesExpectedToWrite * 1.0f));
     }];
    
    [[YourAFHTTPClientSubclass sharedClient] enqueueHTTPRequestOperation:addPhotoRequestOperation];
    

    【讨论】:

    • 抱歉我没有取消这一切ZodioAPIClient shinding?
    • 好的,我已经修改了代码并将其更改为YourAFHTTPClientSubclass - 基本上在使用 AFNetworking (
    • 您是否已将 AFNetworking 添加到您的项目中?您是否创建了自己的 AFHTTPClient 子类?代码中的名称应该是示例 - 您需要创建自己的名称。
    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多