【问题标题】:UIImage replaces with another UIImage not SmoothUIImage 替换为另一个不平滑的 UIImage
【发布时间】:2014-03-25 08:44:46
【问题描述】:

我正在做一个项目,我正在从UIWebView 渲染Images,然后使用自定义UITableViewCell 显示为tableView。当UITableView 第一次出现时...没有要显示的渲染图像,所以我在单元格中显示默认图像(即icon-thumb.png)。图像渲染后,我将重新加载 UITableView。 我这样做如下......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString *reuseIdentifier = @"CustomTableViewID";             CustomTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil)
        {
            cell = (CustomTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:nil options:nil] objectAtIndex:0];
        }

        CGRect mainImageViewFrame = CGRectMake(9, 31, 202, 171);
        cell.mainImageView.frame = mainImageViewFrame;

        [cell setBackgroundColor:[UIColor clearColor]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.transform = CGAffineTransformMakeRotation(M_PI * 0.5);

        cell.slideNumberLbl.textColor = [UIColor whiteColor];
        cell.slideNumberLbl.backgroundColor = [UIColor grayColor];
        cell.slideNumberLbl.layer.cornerRadius = 5.0;
        cell.slideNumberLbl.alpha = 0.5;
        [cell.slideNumberLbl setTextAlignment:NSTextAlignmentCenter];
        cell.slideNumberLbl.text = [NSString stringWithFormat:@"%d",indexPath.row+1];

        [cell.mainImageView setBackgroundColor:[UIColor clearColor]];

        [cell.reflectionImageView setContentMode:UIViewContentModeScaleToFill];
        cell.reflectionImageView.alpha = 0.15;

        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *thumbImagepath = [self.storageDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d_thumb%@",indexPath.row, Image_Extension_JPG]];

        if([manager fileExistsAtPath:thumbImagepath]){
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                __block UIImage *img = [UIImage imageWithContentsOfFile:thumbImagepath];
                dispatch_async(dispatch_get_main_queue(), ^{

                    CGRect mainImageViewFrame = CGRectMake(9, 31, 202, 171);
                    cell.mainImageView.frame = mainImageViewFrame;
                    cell.mainImageView.image = img;
                    [cell.mainImageView setContentMode:UIViewContentModeScaleAspectFit];
                    img = nil;
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                        __block UIImage *reflectionImg = [self reflectedImage:cell.mainImageView withHeight:cell.mainImageView.frame.size.height];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            cell.reflectionImageView.image = reflectionImg;
                            reflectionImg = nil;
                        });
                    });
                });
            });
        }
        else{
            CGRect mainImageViewFrame = CGRectMake(9, 31, 202, 171);
            mainImageViewFrame.origin.y = mainImageViewFrame.origin.y+10;
            mainImageViewFrame.size.height = mainImageViewFrame.size.height-20;
            cell.mainImageView.frame = mainImageViewFrame;

            [cell.mainImageView setContentMode:UIViewContentModeCenter];
            [cell.mainImageView setBackgroundColor:[UIColor whiteColor]];
            cell.mainImageView.image = [UIImage imageNamed:@"icon-thumb.png"];
            [cell.reflectionImageView setBackgroundColor:[UIColor whiteColor]];
            [cell.reflectionImageView setImage:[UIImage imageNamed:@"reflection_temp.png"]];
        }

        return cell;
    }

问题是,当真实图像渲染完成后,默认图像(即 icon-thumb.png)被真实图像替换..替换不流畅,图像替换为 flash/或全部突然,但它需要非常顺利...任何建议,提前谢谢。

【问题讨论】:

标签: ios uitableview ios7 uiimage


【解决方案1】:

你可以使用 CATransition 例如

 CATransition *transition = [CATransition animation];
    transition.duration = 0.2;//duration
    transition.type = kCATransitionFade; //choose your animation
    [yourCell.layer addAnimation:transition forKey:nil];
    [yourCell.mainimageview setImage:img];

【讨论】:

【解决方案2】:

这可能是愚蠢的答案,但您没有尝试为此使用动画吗?

像这样:

        [UIView animateWithDuration:duration animations:^{
            } completion:^(BOOL finished) {
            }
        }];

【讨论】:

    【解决方案3】:

    我会做如下的事情:

    [UIView transitionWithView:cell.mainImageView
                      duration:0.2f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        cell.mainImageView.image = [UIImage imageNamed:@"icon-thumb.png"];
                    } completion:nil];
    

    我发现这种方法稍微优雅一些​​。

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多