UIImage 替换为另一个不平滑的 UIImage
Posted
技术标签:
【中文标题】UIImage 替换为另一个不平滑的 UIImage【英文标题】:UIImage replaces with another UIImage not Smooth 【发布时间】: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/或全部突然,但它需要非常顺利...任何建议,提前谢谢。
【问题讨论】:
***.com/questions/7638831/… 【参考方案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];
【讨论】:
savana 感谢您的回复,但我已经解决了问题。我找到了***.com/questions/2834573/…。但你的回答我也会接受,因为这也可以。【参考方案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];
我发现这种方法稍微优雅一些。
【讨论】:
以上是关于UIImage 替换为另一个不平滑的 UIImage的主要内容,如果未能解决你的问题,请参考以下文章
点击 tableview 单元格时如何更改 UIImage 颜色?