【问题标题】:How to make circle images in NSTableviewcell如何在 NSTableviewcell 中制作圆形图像
【发布时间】:2014-07-28 04:16:59
【问题描述】:

我正在尝试制作一个 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;
}

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

【问题讨论】:

标签: objective-c macos cocoa nstableview


【解决方案1】:

试试下面的代码:

// 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();

【讨论】:

  • 这些类需要导入 uikit 什么的吗?
  • 如果 UIKit 已经在 .pch 文件中,则需要它然后不需要导入,否则是的,你必须
  • 此答案适用于 iOS 而不是 OS X。这些功能在 Cocoa 中不存在。
猜你喜欢
  • 1970-01-01
  • 2014-10-20
  • 2015-10-04
  • 2023-04-02
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多