UIImageView遮罩层是白色的

时间:2011-11-16 14:48:28

标签: iphone uitableview ios4 uiimageview rounded-corners

我正在尝试在UITmageView上创建一个圆角,只在UITableView和底行的顶行的一个指定角上。

在cellForRowAtIndexPath中我有:

NSURL *url = [[NSURL alloc] initWithString:user.image];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"noImageWoman.jpg"]];
[url release];

if (indexPath.row == 0)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
else if (indexPath.row == self.arrayIntrestedPeople.count - 1)
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];

我的roundedCornerOnImage方法:

+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                                   byRoundingCorners:rectCorner
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillColor = [[UIColor whiteColor] CGColor];
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;


    //Don't add masks to layers already in the hierarchy!
    UIView *superview = [imageView superview];
    [imageView removeFromSuperview];
    imageView.layer.mask = maskLayer;

    [superview addSubview:imageView];


    return maskLayer;
}

现在,当加载UITableView时,所有图像都是完整加载的,但不是我应用了图层蒙版的顶部图像和底部图像。它是白色,图像完全白色

当我滚动UITableView以便可以重新创建单元格时,显示UITableView 的顶部和底部图像的图像

为什么会这样?

我在cellForRowAtIndexPath中的漏洞代码是:

- (UITableViewCell *)tableView:(UITableView *)tableVieww cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    GlobalProperties *gp = [GlobalProperties sharedGlobalProperties];
    User *user = [self.arrayIntrestedPeople objectAtIndex:indexPath.row];

    SizeableRoundedTableViewImage *cell = (SizeableRoundedTableViewImage *)[tableVieww dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[SizeableRoundedTableViewImage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSURL *url = [[NSURL alloc] initWithString:user.image];
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"noImageWoman.jpg"]];
    [url release];

    if (indexPath.row == 0)
        cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft];
    else if (indexPath.row == self.arrayIntrestedPeople.count - 1)
        cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft];


        cell.textLabel.text                 = user.username;



    return cell;
}

1 个答案:

答案 0 :(得分:0)

我不确定这是否是真正的解决方案,但看起来你要分配两次mask属性。您在-roundedCornerOnImage:onCorner:内设置它,但也返回掩码,并使用返回值再次设置它。这可能是文档中提到的“未定义结果”。

我的建议是摆脱每次通话的cell.imageView.layer.mask =位。

作为次要说明,在移除它并将其插回原位而不是将其置于所有内容之上之前,谨慎地跟踪UIImageView的索引。以防万一。