gpt4 book ai didi

objective-c - 如何为 CALayer 设置平铺背景图像?

转载 作者:行者123 更新时间:2023-12-03 17:31:46 27 4
gpt4 key购买 nike

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

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];

我已经尝试了注释掉的代码和当前的代码,但都不起作用。谢谢

最佳答案

有一个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 及更低版本的更新

NSColor 转换为 CGColor(未测试):

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

关于objective-c - 如何为 CALayer 设置平铺背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14832618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com