0

当我为 IKBrowserView 设置背景时,背景会根据视图大小调整大小:有没有办法将其设置为平铺图像,重复但从不调整大小?

NSImage*    backgroundImage = [NSImage imageNamed:@"wood-bg2.jpg"];
CALayer*    aLayer = [CALayer layer];
//aLayer.contents = backgroundImage;

CIColor *ci = [[CIColor alloc] initWithColor:[NSColor colorWithPatternImage:backgroundImage]];
CGColorSpaceRef cs = [ci colorSpace];
const CGFloat *comps = [ci components];
CGColorRef cgColor = CGColorCreate(cs, comps);

aLayer.backgroundColor = cgColor;
[itemThumbnailImageBrowserView setBackgroundLayer:aLayer];

我已经尝试了注释掉的代码和当前代码,但都没有工作。谢谢

4

1 回答 1

4

有一种方法NSColor

+ (NSColor *)colorWithPatternImage:(NSImage *)image

所以你可以做这样的事情:

NSImage*    backgroundImage = [NSImage imageNamed:@"wood-bg2.jpg"];
CALayer*    aLayer = [CALayer layer];
aLayer.backgroundColor = [[NSColor colorWithPatternImage:backgroundImage] CGColor]; // - (CGColor) CGColor; is only available in OS X 10.8 and later
[itemThumbnailImageBrowserView setBackgroundLayer:aLayer];

OS X 10.8 及更低版本的更新

转换NSColorCGColor(未测试):

CIColor *ci = [[CIColor alloc] initWithColor:[NSColor colorWithPatternImage:backgroundImage]];
CGColorSpaceRef cs = [ci colorSpace];
const CGFloat *comps = [ci components];
CGColorRef cgColor = CGColorCreate(cs, comps);
于 2013-02-12T12:35:19.367 回答