ios - AFNetworking setUploadProgressBlock 不正常直接达到 100%

标签 ios nsoperation nsoperationqueue uiprogressview afnetworking

我正在使用 AFNetworking Framework 来促进将照片上传到网站。上传工作正常,但我需要在 UIProgressView 中显示进度。问题是,上传开始后进度从 0% 左右每秒移动到 100%,并在一段时间内停留在 100%,具体取决于上传的照片数量。我使用单个请求一次上传 1 到 6 张照片。对这里发生的事情有什么想法吗? AFNetworking 不包括照片的尺寸吗?
它在 NSLOG 中正确显示:

2012-04-03 10:49:45.498 PhotoUp[3689:2207] Sent 32768 of 217931 bytes
2012-04-03 10:49:45.499 PhotoUp[3689:2207] Sent 65536 of 217931 bytes
2012-04-03 10:49:45.501 PhotoUp[3689:2207] Sent 98304 of 217931 bytes
2012-04-03 10:49:45.502 PhotoUp[3689:2207] Sent 131072 of 217931 bytes
2012-04-03 10:49:47.795 PhotoUp[3689:2207] Sent 134310 of 217931 bytes
2012-04-03 10:49:49.070 PhotoUp[3689:2207] Sent 136730 of 217931 bytes
2012-04-03 10:49:50.819 PhotoUp[3689:2207] Sent 139150 of 217931 bytes
2012-04-03 10:49:52.284 PhotoUp[3689:2207] Sent 141570 of 217931 bytes

代码如下:

NSString *numberOfPhotosAsString = [NSString stringWithFormat:@"%d", numberOfPhotos];


NSString *fileBase = @"file0_";
NSString *fileNameBase = @"SourceName_";
NSURL *url = [NSURL URLWithString:@"http://www.mywebsite.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                        numberOfPhotosAsString, @"PackageFileCount",
                        wtID, @"wtID",
                        @"PU", @"type",
                        nil];
for (int i= 0; i < numberOfPhotos; i++)
{
    [params setObject:[fileNames objectAtIndex:i] forKey:[fileNameBase stringByAppendingFormat:@"%i", i]];
}

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/ReceivePhoto.aspx" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 

for (int i = 0; i < numberOfPhotos; i++)
{
    // may want to experiment with the compression quality (0.5 currently)
    NSData *imageData = UIImageJPEGRepresentation([images objectAtIndex:i], 0.5);
    [formData appendPartWithFileData:imageData name:[fileBase stringByAppendingFormat:@"%i", i] fileName:[fileNames objectAtIndex:i] mimeType:@"image/jpeg"];
}
}];

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

[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);
    float progress = totalBytesWritten / totalBytesExpectedToWrite;
    [selectLabel setText:[NSString stringWithFormat:@"%d", progress]];
    [progressView setProgress: progress]; 
    //[testProgressView setProgress:progress];
}];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    [self imageRequestDidFinish];


} 
                                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                      NSLog(@"error: %@",  operation.responseString);

                                  }
 ];

最佳答案

这一行做整数除法:

float progress = totalBytesWritten / totalBytesExpectedToWrite;

您需要将至少一个操作数转换为 float :

float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;

关于ios - AFNetworking setUploadProgressBlock 不正常直接达到 100%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9995068/

相关文章:

ios - 在SwiftUI中形成表单,使用swiftUI在IOS设置中定位用户图片

javascript - react native 重复错误 : Duplicated files or mocks

objective-c - 为什么 nsoperation 连续工作?

ios - 什么可能导致此代码不允许重新进入跳板?

iphone - slider 不符合我的预期

iOS 应用内购买 : Objective-C vs. Swift

objective-c - 为什么 NSOperation 示例代码使用 @try & @catch

iphone - 取消 ASINetworkQueue 中的一个(或多个)特定 ASIHTTPRequest

ios - 获取 AppleWatch 的 GooglePlaces API(以及一般的异步调用)数据(使用 WCSession)

ios - 管理多个 block 调用的最佳方式