2011-12-26 114 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