【问题标题】:Crash while downloading 100 images using NSThread iPhone使用 NSThread iPhone 下载 100 张图像时崩溃
【发布时间】:2011-08-08 12:09:30
【问题描述】:

我目前正在开发一个 iPhone 杂志应用程序,该应用程序从互联网上下载其杂志内容。杂志页面为图片格式,大约有 120 页。

这是我解决问题的方法。

- (void) downloadDergiPages {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *resourcePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [resourcePaths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Dergilerim"];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:indirilenSayi];


for (int i=1; i<=numberOfPages; i++) {

    NSString *url = [NSString stringWithFormat:@"%@getdergipage?dergikey=%@&page=%d", [RepositoryInfo getHostName], indirilenDergi, i];
    NSLog(@"%@", url);

    NSData *receivedData =  [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",i]];
    [receivedData writeToFile:path atomically:YES];
    [receivedData release];

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:i] waitUntilDone:YES];
}


[self performSelectorOnMainThread:@selector(removePopupView) withObject:nil waitUntilDone:YES];
//[self removePopupView];

[pool release];
} 

虽然此代码片段在 iPad 应用程序中运行没有问题,但它会导致应用程序在 44 号结束后崩溃 在 iPhone 中下载页面。这是因为我从日志控制台了解到的内存问题。 我只想获取图像并一张一张地保存到磁盘。我正在释放所有分配“receivedData”。 这段代码可能有什么问题,我怎样才能让它工作?

提前谢谢你..

【问题讨论】:

    标签: iphone uiimage nsthread


    【解决方案1】:

    使用 ASIHTTPRequest API 进行下载。

    【讨论】:

      【解决方案2】:

      你应该使用NSURLConnection

      每次通过委托的方法接收数据时将大块数据保存到磁盘:connection:didReceiveData:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 2015-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-23
        相关资源
        最近更新 更多