2014-03-25 60 views
0

我正在从一个项目中,我从UIWebView渲染Images,然后显示为tableView与自定义UITableViewCell。当UITableView第一次呈现......没有渲染图像显示时,所以我在单元格中显示默认图像(即icon-thumb.png)。一旦图像呈现我正在重新加载UITableView。 我这样做如下...UIImage替换另一个UIImage不光滑

- (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; 
    } 

的问题是,当默认图像(即图标thumb.png)获取与真实图像替换一次真实图像渲染done..the更换不流畅,图像替换为闪光灯或突然全部但它需要非常光滑 ...任何建议,在此先感谢。

+0

http://stackoverflow.com/questions/7638831/fade-dissolve-when-changing-uiimageviews-image/38350024#38350024 –

回答

0

您可以使用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]; 
+0

savana谢谢你的回复,但我已经解决了问题。 我发现http://stackoverflow.com/questions/2834573/how-to-animate-the-change-of-image-in-an-uiimageview。 但你的答案也我会接受,因为这也将工作。 – Suryakant

0

这可能是愚蠢的答案,但你没有尝试使用这个动画?

这样的:

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

我会做类似如下:

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

我发现这个方法稍微更优雅。