2

サーバーからの画像を表示するコードは次のとおりです。画像が読み込まれた後、下にスクロールしてから戻ると、画像が消え、再度読み込まれるまでにさらに時間がかかります。

  dispatch_async(kBgQueue, ^{

        NSString *profileImageStr = [[displayItems objectAtIndex:indexPath.row] objectForKey:@"image_url"];
        NSURL* profileImgURL = [NSURL URLWithString:profileImageStr];
        NSData *profileImgData = [NSData dataWithContentsOfURL:profileImgURL];


        NSString *postPhotoStr = [[displayItems objectAtIndex:indexPath.row] objectForKey:@"image_url"];
        NSURL *postImgURL = [NSURL URLWithString:postPhotoStr];
        NSData *postImageData = [NSData dataWithContentsOfURL:postImgURL];


        dispatch_async(dispatch_get_main_queue(), ^{


            if (postImageData && profileImgData != nil) {


                cell.thumbnailImg.image = [UIImage imageWithData:profileImgData];




                cell.bgImgView.image = [UIImage imageWithData:postImageData];
            }else
            {


                cell.thumbnailImg.image = [UIImage imageNamed:@"noimage.jpg"];




                cell.bgImgView.image = [UIImage imageNamed:@"noimage.jpg"];
            }

        });

    }
                   );
    return cell;
4

1 に答える 1

1

画像のキャッシュを実装する必要があると思います。に画像を保存しNSCacheます。

//instance 
NSCache *imageCache;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *simpleTableIdentifier=nil;
    if([objRepresentative.team count]>0)
    {
        simpleTableIdentifier=[NSString stringWithFormat:@"%@",[objRepresentative.employeeID objectAtIndex:indexPath.row]];
    }
    else
        simpleTableIdentifier = @"ContactTableCell";


     ContactTableCell *cell = (ContactTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ContactTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        NSString *photoString=[NSString stringWithFormat:@"http://www.foo.com/%@",[searchPhoto objectAtIndex:indexPath.row]];

            UIImage *image = [imageCache objectForKey:photoString];
            if (image)
            {
                cell.ProfilePicture.image=image;
            }
            else
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


                    NSURL *url = [NSURL URLWithString:[photoString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

                    if(image)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIImage *small = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
                            cell.ProfilePicture.image=small;
                        });

                        [imageCache setObject:image forKey:photoString];
                    }
                    else
                        cell.ProfilePicture.image=[UIImage imageNamed:@"noPicture.png"];

                });
            }
    }
return cell;
}
于 2013-10-07T05:53:15.657 に答える