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之前。 –