2011-12-26 116 views
0
添加到UIImage的

我有一個UITableViewCell是在drawContentView:繪製圖像,我想陰影添加到它,所以我使用CALayers:無法陰影中的UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    AsyncCell *cell = (AsyncCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[AsyncCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary* obj = [facebookPhotosData objectAtIndex:indexPath.row]; 
    [cell updateCellInfo:obj]; 

    CALayer *sublayer = [CALayer layer]; 
    sublayer.contents = (id)cell.image.CGImage; 
    sublayer.shadowOffset = CGSizeMake(0, 3); 
    sublayer.shadowRadius = 5.0; 
    sublayer.shadowColor = [UIColor blackColor].CGColor; 
    sublayer.shadowOpacity = 0.8; 
    sublayer.frame = CGRectMake(5.0, 5.0, cell.image.size.width, cell.image.size.height); 
    [cell.layer addSublayer:sublayer]; 

    return cell; 
} 

這裏是我的drawContentView方法,如果我在這裏添加子層,他們繼續堆疊:

- (void) drawContentView:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [[UIColor whiteColor] set]; 
    CGContextFillRect(context, rect); 

    NSString* caption = [NSString stringWithFormat:@"%@",[info objectForKey:@"caption"]]; 
    NSString* text = [info stringForKey:@"text"]; 

    CGFloat widthr = self.frame.size.width - 70; 

    [[UIColor grayColor] set]; 
    [text drawInRect:CGRectMake(63.0, 25.0, widthr, 20.0) withFont:system14 lineBreakMode:UILineBreakModeTailTruncation]; 

    if (self.image) { 
     UIImage *imageToDisplay; 
     imageToDisplay = self.image; 
     imageToDisplay = [self imageWithImage:imageToDisplay scaledToSize:CGSizeMake(imageToDisplay.size.width/1.5, imageToDisplay.size.height/1.5)]; 
     CGFloat width; 
     CGFloat height; 
     CGRect r; 
     if (imageToDisplay.size.width < 310 && imageToDisplay.size.height > 290) { 
      imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(0, 20, imageToDisplay.size.width, 250)]; 

     } 
     else if (imageToDisplay.size.width > 310 && imageToDisplay.size.height < 20) { 
      imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 250)]; 
     } 
     else { 
      imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 250)]; 

     } 

     width = imageToDisplay.size.width; 
     height = imageToDisplay.size.height; 
     r = CGRectMake(5.0, 5.0, width, height); 

     [imageToDisplay drawInRect:r]; 

     CALayer *sublayer = [CALayer layer]; 
     sublayer.contents = (id)imageToDisplay.CGImage; 
     sublayer.shadowOffset = CGSizeMake(0, 3); 
     sublayer.shadowRadius = 5.0; 
     sublayer.shadowColor = [UIColor blackColor].CGColor; 
     sublayer.shadowOpacity = 0.8; 
     sublayer.frame = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height); 
     [self.layer addSublayer:sublayer]; 


     //Experimental shadow stuff with images 
     /*CALayer *layer = [CALayer layer]; 
     layer = [CALayer layer]; 
     layer.bounds = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height); 
     layer.position = CGPointMake(150, 140); 
     layer.contents = (id)imageToDisplay.CGImage;  

     layer.shadowOffset = CGSizeMake(0, 2); 
     layer.shadowOpacity = 0.70; 

     [self.layer addSublayer:layer]; 

     [self bezierPathWithCurvedShadowForRect:layer.bounds];*/ 

     [[UIColor blackColor] set]; 
     [caption drawInRect:CGRectMake(0, 270 , 298, 20.0) withFont:system14 lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; 
    } 
} 

當我把代碼放在這個委託方法中沒有任何反應。如果我將它添加到drawContentView,它會繪製陰影,但在我滾動時會不斷添加圖層,這是不正確的。不確定我如何爲我的圖片添加陰影?

回答

0

從提供的描述中,我並不清楚drawContentView方法中有什麼,以及何時調用它,但是您給出的代碼「在滾動時不斷添加圖層」的原因是它確實會將您的子圖層添加到的cellForRowAtIndexPath:每次的tableView時間單元被稱爲(且在此真的經常一邊滾動)

所以,第一個明顯的解決方法是如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    AsyncCell *cell = (AsyncCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[AsyncCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier]; 
     CALayer *sublayer = [CALayer layer]; 
     sublayer.contents = (id)cell.image.CGImage; 
     sublayer.shadowOffset = CGSizeMake(0, 3); 
     sublayer.shadowRadius = 5.0; 
     sublayer.shadowColor = [UIColor blackColor].CGColor; 
     sublayer.shadowOpacity = 0.8; 
     sublayer.frame = CGRectMake(5.0, 5.0, cell.image.size.width, cell.image.size.height); 
     [cell.layer addSublayer:sublayer]; 
    } 

    NSDictionary* obj = [facebookPhotosData objectAtIndex:indexPath.row]; 
    [cell updateCellInfo:obj]; 

    return cell; 
} 

所以,你需要繪製陰影只有一次,大概是當一個單元格被創建時。 個人而言,我寧願在AsyncCell類中有這段代碼。 希望這有助於。

+0

我得到相同的結果,陰影不出現。當我把它放在drawContentView中時,它會被多次添加。更新了原始問題。 – 2011-12-26 20:15:05

+0

drawContentView在哪裏調用? – dariaa 2011-12-26 20:57:23

+0

是否有任何理由你爲什麼使用drawInRect而不是僅僅設置cell.textLabel.text屬性?這是爲了加速滾動而完成的嗎? – dariaa 2011-12-26 21:00:25

0

哦,現在我看到發生了什麼。嘗試將drawContentView中的所有代碼移至drawRect:方法。這應該可以解決問題,因爲只有在setNeedsDisplay標誌被設置時纔會調用drawRect方法,而不是每次用戶滾動。

+0

也沒有工作 – 2011-12-26 22:22:01