异步下载iPad图像

时间:2011-11-05 03:56:34

标签: iphone objective-c cocoa-touch ipad asynchronous

所以我有一个应用程序可以下载并解析带有一些URL的JSON提要。每个请求仅下载4个URL。每个URL都是一个图像URL,我试图显示每个四个。下一个按钮将显示接下来的四个按钮,同时保持前一个按钮可见。

当我运行我的应用程序时,有时它会起作用,有时它不会。我收到EXEC_BAD_ACCESS错误。此外,我认为这不是下载图像的最佳/最有效的方式(即使我是异步进行)。

我是这样做的。我非常感谢有关如何更改应用程序以更有效地下载图像的任何设计建议/建议。

在我的ViewController中,我有以下方法:

- (void)imageSearchController:(id)searchController gotResults:(NSArray *)results {

    AsyncUIImage *imageDownload;

    for (int i = 0; i < [results count]; i++) {

        imageDownload = [[AsyncUIImage alloc] init];

        // Grab the image data in a dictionary
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[results objectAtIndex:i]];        
        // Grab the values in an array
        NSArray *values = [NSArray arrayWithArray:[dictionary allValues]];
        // Get the image's URL in a string - strings at index 3
        NSString *url = [NSString stringWithString:[values objectAtIndex:3]];

        imageDownload.delegate = self;
        [imageDownload downloadImageFromURL:url];
        [imageDownload release];
    }
}

此方法是下载JSON源后立即调用的方法(使用NSURLConnection - 这是委托方法)。它会创建一个AsyncUIImage对象,然后将每个图像的URL传递给该对象以进行下载和显示。

我的AsyncUIImage类是处理图像下载的对象。它有以下方法:

首先,开始下载的方法如下:

- (void)downloadImageFromURL:(NSString *)url {


    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        data = [[NSMutableData data] retain];
    } 

    else {
        NSLog(@"error in connection");
        [theConnection release];
    }
}

然后是通常的NSURLConnection委托方法。 connectionDidFinishLoading:方法如下:

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {    
    self.image = [UIImage imageWithData:data];
    [self.delegate imageDidLoad:self.image];
}

此方法通知委托方法已下载图像。回到ViewController,imageDidLoad委托方法将显示如下图像:

- (void)imageDidLoad:(UIImage *)image {

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset, yOffset, 192, 192)];
    imageView.image = image;
    [imageView release];

    [self.view addSubview:imageView];
    xOffset = xOffset + 192;

    if (count != 0 && count % 4 == 0) {
        yOffset = yOffset + 192;
        xOffset = 0;
    }

    count++;
}

我对iOS平台上的异步下载没有丰富的经验,但是从编程经验和iOS体验,我相信我所做的事情有一些重大缺陷。

有没有人有任何想法/建议?任何反馈都非常感谢。

谢谢。

2 个答案:

答案 0 :(得分:1)

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset, yOffset, 192, 192)];
    imageView.image = image;
    [imageView release];

    [self.view addSubview:imageView];

当您将imageView传递给addSubview:时,它已被取消分配。您需要在 之后将其添加为self.view的子视图。

如果您使用Google objective-c内存管理,您会找到许多初学者指南,以帮助您了解retainrelease

答案 1 :(得分:1)

也许不是您正在寻找的答案,但我已将此库用于此目的:HJCache 我制作了一个Twitter客户端应用程序,我自己的下载异步图像的实现也给我带来了很多问题。

更改为此库后,一切顺利,如果您想添加额外的功能或其他任何内容,也可以修改它。

它还可以让您缓存图像,同时下载桌面视图上的多个图像也没有问题。