как загрузить изображение в фоновом режиме в UITableCell в приложении iphone

Я разработал приложение 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];

любая идея, пожалуйста, заранее спасибо ...


person Nauman.Khattak    schedule 09.07.2010    source источник
comment
возможный дубликат изображений с отложенной загрузкой в ​​UITableViewCell   -  person progrmr    schedule 09.07.2010


Ответы (4)


Повторная публикация ... Вы также должны посмотреть на отложенную загрузку таблицы с помощью примера кода Apple http://developer.apple.com/iphone/library/samplecode/LazyTableImages/Introduction/Intro.html

Обновите еще один пример: http://www.markj.net/iphone-asynchronous-table-image/

person iwasrobbed    schedule 09.07.2010

Ну, ребята, я сделал это, код ...

- (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..

Спасибо декану Вомбурну, который дал мне этот код ....

person Nauman.Khattak    schedule 09.07.2010
comment
Пожалуйста, выполните поиск в следующий раз перед заданием вопроса. Для тех, кто пытается помочь, это напрасная трата усилий, если мы публикуем ответы, а вы просто берете код в другом месте после выполнения простого поиска. - person iwasrobbed; 10.07.2010

И взгляните на этот документ также от Apple.

http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html

Чаще всего я использую NSURLConnection для загрузки.

person Sandro Meier    schedule 09.07.2010

Вы можете использовать этот способ:

((UIImageView *)cell.backgroundView).image = bgImage;
((UIImageView *)cell.selectedBackgroundView).image = bgImage;
person Son Nguyen    schedule 13.07.2010
comment
Ты не ответил на вопрос. Вопрос был в том, как загрузить изображение в фоновом режиме, а не установить фоновое изображение. - person Johan Karlsson; 10.03.2014