2014-03-25 95 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]; 

我發現這個方法稍微更優雅。