Я разработал приложение RSS. в ячейке моей таблицы я показываю изображения, которые я получаю по ссылке imge в RSS-потоке и заголовке. Изображения загружаются успешно, но проблема в том, что мое приложение зависает. как есть способ загрузить изображение в фоновом режиме. мой текущий код.
int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: @"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image=[img autorelease];
любая идея, пожалуйста, заранее спасибо ...
Ответ 2 · рейтинг 1 · Nauman.Khattak
Ну, ребята, я сделал это, код ...
- (void) loadImage:(id)object {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Get the data to pass in
NSDictionary *dict = (NSDictionary *)object;
// Get the cell and the image string
NSString *imgstring = [dict objectForKey:@"imgstring"];
MyfirstCell *cell = [dict objectForKey:@"cell"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.myimnage.image = img;
[pool release];
}
Я вызову указанный выше метод в своем коде ...
if(searching) {
//cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
int blogEntryIndex = [indexPath indexAtPosition: [indexPath length] -1];
// Tell your code to load the image for a cell in the background
NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex] objectForKey: @"image"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell, @"cell", imgstring, @"imgstring", nil];
[self performSelectorInBackground:@selector(loadImage:) withObject:dict];
//background code end here..
Спасибо декану Вомбурну, который дал мне этот код ....
Ответ 3 · Sandro Meier
И взгляните на этот документ также от Apple.
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html
Чаще всего я использую NSURLConnection для загрузки.
Ответ 4 · рейтинг -2 · Son Nguyen
Вы можете использовать этот способ:
((UIImageView *)cell.backgroundView).image = bgImage;
((UIImageView *)cell.selectedBackgroundView).image = bgImage;