我正在使用tableview从url加载图像,但在tableview中重复显示图像

时间:2014-07-12 13:59:59

标签: ios objective-c uitableview

我正面临图像重复问题,如果图像未加载完成则尝试向下滚动或向上滚动,然后图像重复与完成下载的图像相同。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"galCustom";

    GTInstaTableViewCell *cell = (GTInstaTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    for (UIView *view in cell.imageView.subviews)
    {
        [view removeFromSuperview];
    }

    cell._imageLoadingView.hidden = NO;
    [cell._imageLoadingView startAnimating];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    GTInstaTableViewCell *cell1 = (GTInstaTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    //Gallery Photo

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
    {
        cell.imageView.image = nil;
        NSString *strImgname = [[arrTabledata objectAtIndex:indexPath.row] objectForKey:@"photo_name"];
        imgPname = [IMAGE_URL stringByAppendingString:strImgname];

        NSString *aPath = [self.thumbDirectory stringByAppendingPathComponent:strImgname];

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath:aPath]) 
        {
            NSURL *url = [NSURL URLWithString:imgPname];
            NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];

            float containerWidth = 648.0;

            //Image Resize
            UIImage *image = [UIImage imageWithData:imgData];

            float width = image.size.width;
            float height= image.size.height;
            float thumbWidth = width/containerWidth;
            float imageHeight = height/thumbWidth;

            CGSize sacleSize = CGSizeMake(containerWidth, imageHeight);
            UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
            [image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
            UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            NSData *imageData = UIImagePNGRepresentation(resizedImage);

            [imageData writeToFile:aPath atomically:YES];
         }

         UIImage *image = [UIImage imageWithContentsOfFile:aPath];

         dispatch_async(dispatch_get_main_queue(), ^
         {
             if (image) 
             {
                 cell.imageView.image = image;
                 cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
                 cell.imageView.clipsToBounds = YES;
                 [cell1._imageLoadingView stopAnimating];
                 cell1._imageLoadingView.hidden = YES;
             }
             else
             {
                 cell.imageView.image = [UIImage imageNamed:@"noImage.jpg"];
             }
         });
    });   
}

1 个答案:

答案 0 :(得分:-1)

您应该实施GTInstaTableViewCell.m方法:

- (void) prepareForReuse
{
    self.imageView.image = nil;
}