0

表示されたすべての画像を読み込んUITableViewCellでいるときに問題がありますが、問題は非常に遅いです。これはコードですimagesUTableView scroll

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    customSearchesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];

    if(cell == nil)
    {
        cell =[[customSearchesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"];        
    }    

    searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row];
    cell.location.text =searchobjectval.location;
    cell.title.text = searchobjectval.title;
    cell.cost.text = searchobjectval.roomCost;
    cell.desc.text =searchobjectval.description;

    [tableView setSeparatorColor:[UIColor blueColor]];

    UIView *myView = [[UIView alloc] init];
    if (indexPath.row % 2)
    {
        UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1];
        myView.backgroundColor = clr;
    }
    else
    {
        myView.backgroundColor = [UIColor whiteColor];
    }
    cell.backgroundView = myView;

    NSLog(@" search object image %@",searchobjectval.imgLink);

    UIImage *ret = [self imageNamed:[NSString stringWithFormat: @"http://192.95.76.78/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] cache:YES andsearchObj:searchobjectval];

    cell.imgVw.image = ret;

    return cell;
}

    // images caching in dictionary
    - (UIImage*)imageNamed:(NSString*)imageNamed cache:(BOOL)cache andsearchObj:(clsSearch *)searchObj
    {
         UIImage* retImage = [staticImageDictionary objectForKey:imageNamed];
        @try
        {
            if (retImage == nil)
            {
                retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];
                if (cache)
                {
                    if (staticImageDictionary == nil)
                        staticImageDictionary = [NSMutableDictionary new];

                    [staticImageDictionary setObject:retImage forKey:imageNamed];

                }               
            }
        }
        @catch (NSException *exception)
        {
            NSLog(@"exception %@",exception);
        }
        @finally {
            NSLog(@"finally block execute here");
        }

        return retImage;
    }

この問題を解決する方法を教えてください。コードを内部に貼り付けましたif (cell == nil)が、まだ成功していません。

4

5 に答える 5

0

retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];

セルが表示されるたびに、この行はサーバーからのデータ (画像) のフェッチを開始します。のスクロールを開始するだけでなくUITable、リクエストがサーバーに送信されて画像が取得されるため、 を使用してサーバーから画像を取得する必要がありますNSThread

ここで私のコードを試してみてください。役に立つ場合は賛成してください。 サーバーから画像を非同期で取得する方法

于 2013-05-16T10:21:26.290 に答える
0

[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];メソッド:は で実行されることに注意してくださいmain thread

イメージを非同期でダウンロードするには、NSOperatioQueue Samople コードまたは ASIHTTP ライブラリを参照してください。

于 2013-05-16T10:22:16.107 に答える
0

問題は、テーブルセルへの画像の同期ロードです。非同期にすると、正常に動作します

これを使って

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
 dispatch_async(queue, ^{
       NSString *strURL = url here;
       NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
       UIImage *image = nil;
       if(data)
        image = [UIImage imageWithData:data];
      dispatch_sync(dispatch_get_main_queue(), ^{
           //now use image in image View  or anywhere according to your requirement.
           if(image)
             yourImgView = image
      });
 });
于 2013-05-16T10:20:18.870 に答える
0

SDWebImageWebサーバーから画像をロードするために使用します。

于 2013-05-16T10:21:14.533 に答える
0

画像を非同期にロードするだけでなく、tableViewのスクロールを非常に高速かつ高速にするSDWebImageライブラリを使用できます。

ここからライブラリを取得できます.. https://github.com/nicholjs/SDWebImage-with-loading-progress

于 2013-05-16T10:25:27.467 に答える