2014-09-03 23 views
1

我正在使用UITableViewCell開發iOS應用程序。UITableViewCell中的圖像在底部附近慢慢變暗

我想在一個單元格中的圖像緩慢地轉向圖像底部附近的黑暗。

我寫下以下內容。 但是,它不能很好地工作。

你能告訴我如何解決這個問題嗎?

※我使用SDWebImage(https://github.com/rs/SDWebImage)作爲異步圖片下載器。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSString *cellIdentifer = @"myCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath]; 

    if(cell==nil){ 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer]; 
    } 

    UIImageView *NewsImage = (UIImageView *)[cell viewWithTag:1]; 
    NSURL *imageURL = [NSURL URLWithString:imageUrlString]; 
    UIImage *placeholderImage = [UIImage imageNamed:@"placeImage.jpg"]; 

    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.frame = NewsImage.frame; 
    gradient.colors = @[(id)[[UIColor clearColor] CGColor], 
         (id)[[UIColor blackColor] CGColor]]; 
    [NewsImage.layer insertSublayer:gradient atIndex:0]; 

    [NewsImage setImageWithURL:imageURL placeholderImage:placeholderImage]; 

    return cell; 

} 
+1

你不返回tableviewcell。 – 2014-09-03 04:25:03

+0

是的,我已經返回tableviewcell。我將它添加到上面的代碼中。 – supermonkey 2014-09-03 04:59:00

+0

確切的問題是什麼?圖像未加載?或者只是圖像不在底部變黑。 – jnix 2014-09-03 05:08:19

回答

0

需要檢查的幾件事:確保imageURLString它實際上是一個圖像。 二,將setImageWithURL(已棄用)更改爲sd_setImageWithURL。同時將gradient.frame更改爲NewsImage.bounds。如果仍然不起作用,請嘗試移動SDWebImage語句下方的漸變片段。

+0

在第三次檢查時(將gradient.frame更改爲NewsImage.bounds),我可以解決問題。非常感謝你! – supermonkey 2014-09-03 07:06:32

0

我認爲這個問題是:

NSURL *imageURL = [NSURL URLWithString:imageUrlString]; 

是什麼在 「imageURLString」

如果你只是試圖把圖像文件在你的項目,你可以嘗試:

NSURL * url = [[NSBundle mainBundle] URLForResource:@「imageFileName」withExtension:@「ext」];

+0

imageURLString是url(http:// ...)。原始圖像(但不是漸變)出現,所以這部分是沒有問題的。 – supermonkey 2014-09-03 06:03:37

+0

@szakira原始圖像是佔位符圖像? – Sebyddd 2014-09-03 06:08:01

+0

NO。原始圖像是來自imageURLString的圖像。 – supermonkey 2014-09-03 06:08:57