Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何从异步请求中获取剩余上载时间?_Iphone_Ios_Progress Bar_Image Uploading - Fatal编程技术网

Iphone 如何从异步请求中获取剩余上载时间?

Iphone 如何从异步请求中获取剩余上载时间?,iphone,ios,progress-bar,image-uploading,Iphone,Ios,Progress Bar,Image Uploading,我正在尝试将一个图像从iPhone上传到服务器。我可以这样做,但我需要显示一个指标。我的代码是 NSData *imageData = UIImageJPEGRepresentation(newImage, 90); // setting up the URL to post to NSString *urlString = [NSString stringWithFormat:@"URL",[[formater stringFromDate:now] stringByAddingPercen

我正在尝试将一个图像从iPhone上传到服务器。我可以这样做,但我需要显示一个指标。我的代码是

NSData *imageData = UIImageJPEGRepresentation(newImage, 90);

// setting up the URL to post to
NSString *urlString = [NSString stringWithFormat:@"URL",[[formater stringFromDate:now] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
  NSString *boundary = [NSString stringWithFormat:@"---------------------------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 stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
这是启动请求的代码

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
 }];
那么,如何计算剩余时间,以便显示指示器?
谢谢您的时间。

我相信您希望显示进度条之类的内容。我建议使用MKNetworkKit,它为您提供了许多网络管理工具,例如异步连接的进度。检查此项。

我相信您希望显示进度条之类的内容。我建议使用MKNetworkKit,它为您提供了许多网络管理工具,例如异步连接的进度。选中此项。

您可以使用此连接委托方法:

- (void)       connection:(NSURLConnection *)connection
          didSendBodyData:(NSInteger)bytesWritten
        totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
(我应该如何包装这么长的参数名?
它不在
NSURLConnectionDataDelegate
的文档中,但是如果您检查公共标题
NSURLConnection.h
,它就在那里。所以不用担心使用它

从文档URL加载系统编程指南>使用NSURLConnection:

估计上传进度

您可以使用
连接:didSendBodyData:TotalBytesWrite:totalBytesExpectedToWrite:
委托方法估计HTTP POST上载的进度。请注意,这不是上载进度的精确度量,因为连接可能失败或连接可能遇到身份验证挑战


您可以使用此连接委托方法:

- (void)       connection:(NSURLConnection *)connection
          didSendBodyData:(NSInteger)bytesWritten
        totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
(我应该如何包装这么长的参数名?
它不在
NSURLConnectionDataDelegate
的文档中,但是如果您检查公共标题
NSURLConnection.h
,它就在那里。所以不用担心使用它

从文档URL加载系统编程指南>使用NSURLConnection:

估计上传进度

您可以使用
连接:didSendBodyData:TotalBytesWrite:totalBytesExpectedToWrite:
委托方法估计HTTP POST上载的进度。请注意,这不是上载进度的精确度量,因为连接可能失败或连接可能遇到身份验证挑战


可能是@lxt的副本谢谢你的时间,但我不是在找。我知道NSURLconnection委托方法,但我正在单独的线程中在一个块内进行上载。可能是@lxt的副本谢谢您的时间,但我不寻找。我知道NSURLconnection委托方法,但我在单独的线程中在一个块内进行上载。您好@CainaSouza感谢您的回复。我不想为这个小任务使用任何第三方库。该工具包将用于复杂任务。您好@CainaSouza,感谢您的回复。我不想为这个小任务使用任何第三方库。该工具包将用于复杂任务。您好@iMartin谢谢您的回答。我已经在代码中添加了这个方法,但是没有调用这个方法。我还在.h文件中添加了
nsurlconnectionelegate
。这是由于以下代码造成的吗<代码>[NSURLConnection sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue]完成处理程序:^(NSURResponse*响应,NSData*数据,NSError*错误){NSLog(@“已完成,状态代码:%i”,[(NSHTTPURResponse*)响应状态代码];}是,您必须使用其他方法,因为您需要设置委托。例如,使用
-initWithRequest:delegate:
,然后使用
-start
。然后实现一些其他的委托方法来检查是否完成或错误。我已经在代码中添加了这个方法,但是没有调用这个方法。我还在.h文件中添加了
nsurlconnectionelegate
。这是由于以下代码造成的吗<代码>[NSURLConnection sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue]完成处理程序:^(NSURResponse*响应,NSData*数据,NSError*错误){NSLog(@“已完成,状态代码:%i”,[(NSHTTPURResponse*)响应状态代码];}是,您必须使用其他方法,因为您需要设置委托。例如,使用
-initWithRequest:delegate:
,然后使用
-start
。然后实现一些其他委托方法来检查完成或错误。