Ios AFNetworking setUploadProgressBlock不正常直接达到100%
我正在使用AFNetworking框架来促进照片上传到网站。上传工作正常,但我需要在UIProgressView中显示进度。问题是,上传开始后,进度从每秒0%移动到100%左右,并根据上传的照片数量在一段时间内保持100%。我使用单个请求一次上传1到6张照片。你知道这里发生了什么吗?不包括照片的大小吗?Ios AFNetworking setUploadProgressBlock不正常直接达到100%,ios,nsoperation,nsoperationqueue,uiprogressview,afnetworking,Ios,Nsoperation,Nsoperationqueue,Uiprogressview,Afnetworking,我正在使用AFNetworking框架来促进照片上传到网站。上传工作正常,但我需要在UIProgressView中显示进度。问题是,上传开始后,进度从每秒0%移动到100%左右,并根据上传的照片数量在一段时间内保持100%。我使用单个请求一次上传1到6张照片。你知道这里发生了什么吗?不包括照片的大小吗? 它在NSLOG中正确显示: 2012-04-03 10:49:45.498 PhotoUp[3689:2207] Sent 32768 of 217931 bytes 2012-04-03 10
它在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);
}
];
NSString*numberoftofsasstring=[NSString stringWithFormat:@“%d”,numberoftofstring];
NSString*fileBase=@“file0”;
NSString*fileNameBase=@“SourceName_”;
NSURL*url=[NSURL URLWithString:@”http://www.mywebsite.com"];
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
NSMutableDictionary*参数=[NSMutableDictionary Dictionary WithObjectsAndKeys:
NumberOfPhotoAssString,@“PackageFileCount”,
wtID,@“wtID”,
@“PU”、@“type”,
零];
对于(int i=0;i
此行进行整数除法:
float progress = totalBytesWritten / totalBytesExpectedToWrite;
您至少需要强制转换一个操作数才能浮动:
float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
我试图发布那个答案,但没有足够的代表在8小时内回答自己的问题。你是对的,这是一个多么幼稚的错误!谢谢