Bezdepbonusu7Программирование: статьи и ответы

Вопрос

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

Автор вопроса: Nauman.KhattakОтветов: 4

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

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

  • возможный дубликат изображений с отложенной загрузкой в ​​UITableViewCell progrmr 09.07.2010

iphoneobjective-cuitableview

Ответы

Ответ 1 · рейтинг 4 · iwasrobbed

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

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

Ответ 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;

Перевод вопроса и ответов сообщества Stack Overflow, лицензия CC BY-SA. Оригинал на Stack Overflow.

Похожие материалы