2013-02-12 69 views
0

当我为IKBrowserView设置背景时,根据视图大小调整背景大小:是否有将它设置为平铺图像的方法,重复但未调整大小?如何为CALayer设置平铺背景图像?

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

还有的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); 
+0

感谢,由于某种原因,colorWithPatternImage行给了我这样的警告:从兼容的指针类型 – aneuryzm 2013-02-12 12:40:31

+0

传递参数1我不知道为什么,因为我经过NSImage中为你这是正确的内容类型 – aneuryzm 2013-02-12 12:40:47

+0

啊,这是因为backgroundColor需要一个CGColor而不是一个NSColor – aneuryzm 2013-02-12 12:44:50