3

我正在开发一个应用程序,我使用以下代码检查 url。

-(无效)存在
{
    NSString *strImg = @"一些网址";


    NSURL *aImgUrl = [NSURL URLWithString:strImg];

    NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:strImg];
    [imgRequest setHTTPMethod:@"HEAD"];

    imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self];
}

这就是我得到回应时所做的。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    if ([(NSHTTPURLResponse *)response statusCode] == 200)
    {
        // 网址存在
        appDel.isExists = TRUE;
        温度=假;
        [自加载数据];
        NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
    }
    否则如果([(NSHTTPURLResponse*)响应状态码] == 404)
    {
        //url不存在
        appDel.isExists = FALSE;
        温度=真;
        [自加载数据];
        NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]);
    }
}

这是我的加载数据代码

-(无效)加载数据
{


    结果=假;

    isReqSent = 假;

    做
    {
        如果(appDel.isExists == TRUE)
        {
            NSURL *aTxtUrl = [NSURL URLWithString:apiText];
            NSURL *aImgUrl = [NSURL URLWithString:apiImage];

            NSURLRequest *imgRequest = [NSURLRequest requestWithURL:aImgUrl];
            NSURLRequest *txtRequest = [NSURLRequest requestWithURL:aTxtUrl];

           [NSURLConnection sendAsynchronousRequest:txtRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
             {
                 如果(数据)
                 {
                         NSString *strTxt = [[NSString alloc]initWithData:数据编码:NSUTF8StringEncoding];
                         [lblTime setText:strTxt];
                          [policyImgWebView loadRequest:imgRequest];

                         结果=真;                     
                 }

             }];

        }

        如果(结果)
        {
            appDel.isExists = TRUE;
        }
        别的
        {
            如果(!isReqSent)
            {
                NSString *soapMessage = @"我的肥皂信息";

                NSString *soapAction = @"我的肥皂网址";


                [objWebServiceParser xmlParsing:soapMessage:soapAction:Number];
                isReqSent = TRUE;
            }

            }
        如果(温度)
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                [自背景进程];
            });
        }

    } 而(结果 == FALSE);

这是我的后台进程

-(void)backgroundProcess
{

    NSString *strImg = @"一些网址";

    NSURL *aImgUrl = [NSURL URLWithString:apiImage];

    NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl];
    [imgRequest setHTTPMethod:@"HEAD"];

    imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self];

}

我不知道我在哪里做错了,任何人都可以指导我吗?

我希望后台进程继续运行,直到它获取数据,当收到的数据 appdel.isExists 为真并进入主线程以更新 ui 时。

我尝试了 GCD &performSelectorInTheBackground但我没有做对 & 我收到NSThread失败错误 35。


它与 NSURLSessionDownloadTask 一起工作,但仍在寻找更好的选择。

4

2 回答 2

2

不要NSURLConnection在后台调用。NSURLConnection将自动工作。由于您没有发送异步请求,它将在后台工作并且不会挂起您的主线程。在您的加载数据中删除调度。你有:

if (temp)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [self backgroundProcess];
        });
    }

做了:

if (temp)
    {
        [self backgroundProcess];
    }

您不需要使用调度。

于 2014-02-26T09:06:53.920 回答
0

要将图像加载到您UIImageView的服务器中,您可以使用以下框架。

SDWebImage

您将获得completionBlock, progressBlock& 更多可用于更新 UI 的属性。它会分块通知您,然后您可以在 UI 上更新您想要的任何内容。

样本:

如果imageURL有效,只需加载图像,就像这段代码一样简单(这是你的情况)

UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]];

它也有丰富的方法,如下所示

UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL] 
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]
           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) 
           {
               // in "image" you'll get the downloaded image from the URL
               ... completion code here ...

           }];
于 2014-02-28T03:21:56.440 回答