如何在从图像URL数组下载/转换数据时显示UIProgressView?

时间:2014-04-24 14:59:06

标签: ios iphone objective-c nsurlconnection uiprogressview

我有一个URL数组。我从服务器获取此URL并希望将该图像URL数据保存到我的SQL数据库中,因此在将此图像URL转换为数据然后将该数据保存到我的数据库时,希望使用UIProgressView显示Progress。

请让我知道这件事。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。

ASIHTTPRequest 是最佳选择。

对于项目中的设置 ASIHTTPRequest ,请查看此link

在项目导入中导入ASIHTTPRequest类后,您的类中的两个类ASIHTTPRequest.h和ASINetworkQueue.h然后定义UIProgressView * proGress& 将ASINetworkQueue * networkQueue对象放入.h文件中。

现在.m文件的代码是

- (void)viewDidLoad
{
    [super viewDidLoad];

    //adata=[[NSMutableArray alloc] init];

    [[self networkQueue] cancelAllOperations];
    // Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
    [self setNetworkQueue:[ASINetworkQueue queue]];
    [[self networkQueue] setDelegate:self];
    [[self networkQueue] setDownloadProgressDelegate:pgress];

    [[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
    [[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

    int i;
    for (i=0; i<2; i++) {
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]];
        [[self networkQueue] addOperation:request];
    }
    [[self networkQueue] go];

}
- (void)requestFinished:(ASIHTTPRequest *)request
{

    //NSData *response = [request responseData];

    if ([[self networkQueue] requestsCount] == 0) {

        [self setNetworkQueue:nil];
    }
    //... Handle success
    //[adata addObject:response];
    NSLog(@"Request finished =%f",[pgress progress]);
}


- (void)queueFinished:(ASIHTTPRequest *)request
{
    if ([[self networkQueue] requestsCount] == 0) {
        [self setNetworkQueue:nil];
    }
    NSLog(@"Queue finished =%f",[pgress progress]);
    //img.image=[UIImage imageWithData:[adata objectAtIndex:0]];
}

- (void)requestWentWrong:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
      NSLog(@"error =%@",error);
}