0
这是下载图像的功能,在此功能中我必须更新UI,显示总图像和计数器值(下载图像)的UILabels。在下载iOS中的图像时更新UI(标签)
那就是为什么我要调用一个线程。
-(void) DownloadImages
{
NSLog(@"Images count to download %lu",(unsigned long)[productID count]);
if ([productID count] > 0)
{
// Document Directory
NSString *myDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
for (i = 0; i < [productID count]; i++)
{
[NSThread detachNewThreadSelector: @selector(UpdateLabels) toTarget:self withObject:nil];
// [self UpdateLabels]; this does not work….
NSLog(@"image no %d", i);
//[self Status];
NSString *imageURL = [NSString stringWithFormat:@"http://serverPath/%@/OnlineSale/images/products/%@img.jpg",clientSite,[productID objectAtIndex:i]];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
// Reference path
NSString *imagePath = [NSString stringWithFormat:@"%@/%@-1-lrg.jpg",myDirectory,[productID objectAtIndex:i]];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[imageData writeToFile:imagePath atomically:YES];
}
NSLog(@"Downloading images completed...");
}
}
问题: - 在这里为每一个新的形象,我有每当一个新的NSThread时间打电话,可能有数千张图片,它看起来不好会有数千个线程。
- 在模拟器我测试为图像,但在我的ipad当测试它给出了错误,,, 应用退出并终止,因为该存储器的压力。
我想这个错误是由于这种新的线程每次调用的方法。
我在做什么错在这里。?????????????
我已经搜索有两种不同的选择,但我不知道哪一个是正确的选择,
选项包括:
- [self performSelectorInBackground:@selector(UpdateLabels) withObject:nil];
- [NSThread detachNewThreadSelector: @selector(UpdateLabels) toTarget:self withObject:nil];
,我也想知道,如果我使用
[NSThread cancelPreviousPerformRequestsWithTarget:self];
就在选择器的调用之后,它是否正确或不是。
539图像下载后,我得到这个错误。 –
以及我如何知道maxConcurrentOperationCount? –
我用一些例子更新我的答案。但我建议阅读有关NSOperationQueue之前。 –