如何在NSTableviewcell中制作圆形图像

时间:2014-07-28 04:17:00

标签: objective-c macos cocoa nstableview

我正在尝试创建一个mac应用程序,在我的tableview中我有一个标签和imageview

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    NSString *cellId = @"Feature";
    NSTableCellView *cellView =[tableView makeViewWithIdentifier:cellId owner:self];

    if([tableColumn.identifier isEqualToString:@"FeaturesColumn"]){
        if([cellId isEqualToString: @"Feature"]){
            CALayer *imageLayer = cellView.imageView.layer;
            [imageLayer setCornerRadius:30.0f];
            [imageLayer setBorderWidth:1];
            [imageLayer setMasksToBounds:YES];
            [cellView.imageView setLayer:imageLayer];
            [cellView.textField setStringValue:[_features objectAtIndex:row]];
            return cellView;
        }

    }
    return cellView;
}

但每当我运行模拟时,我得到的只是矩形图像

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

// Get your image somehow
    UIImage *image = [UIImage imageNamed:@"image.jpg"];

    // Begin a new image that will be the new image with the rounded corners
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);

    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
    cornerRadius:10.0] addClip];
    // Draw your image
    [image drawInRect:imageView.bounds];

    // Get the image, here setting the UIImageView image
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();