使用AFNetworking发送多张图像 [英] Sending more than one image with AFNetworking

查看:51
本文介绍了使用AFNetworking发送多张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个消息传递应用程序,用户也可以彼此发送图片.
当用户发送多张图片时,我会并行发送它们(在发送第二张图片之前,我不会等待第一张图片完成上传)

I'm developing a messaging app, and users can also send pictures to one another.
When a user sends more than one picture I send them in parallel (I don't wait for the first one to finish upload before I send the second one)

在移到AFNetworking之前,我已经成功地通过ASIFormDataRequest进行了此操作,实际上,如果我发送了2张图像,则它们都是并行传输的,并已成功地传递给了另一个用户.

Before moving to AFNetworking I succeeded in doing this with ASIFormDataRequest, and indeed if I sent 2 images, both of them were transmitted in parallel and successfully delivered to the other user.

当我尝试通过AFNetworking进行此操作时,出现了一些奇怪的行为.
我将尝试描述user1也发送两个图像user2的情况:

When I try doing this with AFNetworking, I get some strange behavior.
I'll try to describe the case were user1 send two images too user2:

  1. 用户1发送图片1->一切看起来都正常,我可以看到上传进度.
  2. 用户1然后发送图片2->看起来还可以,我可以看到两个图片的上传进度
  3. image1上传完成-> user2得到了损坏的图像,看起来像是image1和image2的组合!
  4. image2上传完成-> user2成功获取image2

这就是我发送图片的方式

This is how I send an image

- (void)sendImageMsgWithPath:(NSString *)path
                       image:(UIImage *)image
                     success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
                    progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

    // create the request

    NSURLRequest *request = [[AppClient sharedClient] multipartFormRequestWithMethod:@"POST" path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
                             {
                                 [formData appendPartWithFileData:imageData name:@"image_name" fileName:@"image_name.jpg" mimeType:@"image/jpeg"];
                             }];


    // create the operation

    AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];

    // set progress block

    [operation setUploadProgressBlock:progress];

    //set completion blocks

    [operation setCompletionBlockWithSuccess:success failure:failure];

    // set it to work in background

    [operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:nil];

    // add it to the operations queue

    [[AppClient sharedClient] enqueueHTTPRequestOperation:operation];
}

***两次图像上传的路径都相同:

*** the path for both image uploads is the same:

路径= @"FrontEnd?cmd = sendimage&fromuserid = 3&touserid = 1&"

path = @"FrontEnd?cmd=sendimage&fromuserid=3&touserid=1&"

它将被添加到baseURL中以创建完整的URL:

it will be added to the baseURL to create the coplete URL:

@" http://somename .myftp.org:8080/web_proj/FrontEnd?cmd = sendimage& fromuserid = 3& touserid = 1 "

这是我发送的图像:
image1

This are the images I sent:
image1

image2

image2

图像损坏

推荐答案

如何创建传递到sendImageMsgWithPath:方法中的UIImage对象?您是否使用imageWithData :(或类似方法)创建它们?如果是这样,我在尝试重用NSMutableData时遇到了类似的问题.看来,即使在创建UIImage之后,子系统仍需要在以后读取该数据.而且,如果您此后重新使用了该NSMutableData,则该映像将被破坏.

How are you creating the UIImage objects that get passed into your sendImageMsgWithPath: method? Are you creating them using imageWithData: (or similar)? If so, I have seen problems like this when trying to reuse an NSMutableData. It seems that even after you create the UIImage, the subsystem still needs to read that data at a later time. And if you have since reused that NSMutableData, the image will be corrupted.

在这种情况下,建议您使用新的NSMutableData创建每个UIImage.

If this is the case, I recommend that you use a new NSMutableData to create each UIImage.

这篇关于使用AFNetworking发送多张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆