在实时创建模式? [英] Creating pattern on-the-fly?
问题描述
有没有办法在Cocoa中动态地创建彩色填充模式?
特别是不要通过
$从图像文件中使用固定模式b$ b
NSColor * fillPattern = [NSColor colorWithPatternImage:patternImage];
我想在运行时动态选择合适的颜色来创建模式。
背景是通过在其顶部的相反颜色上呈现条纹或正方形来突出显示一个彩色对象 - 无论在上下文中是否相反 ,但这是一个不同的故事。
应用于绘图应用程序中潜在的数百个对象,它需要一个相当快的方法,所以我想只是交换 patternImage
( )
<为什么不绘制到内存中的图片并将其用于你的模式?
> NSImage * patternImage = [[NSImage alloc] initWithSize:someSize];
[patternImage lockFocus];
//绘制你的模式
[patternImage unlockFocus];
NSColor * patternColor = [NSColor colorWithPatternImage:patternImage];
//使用模式颜色做一些事情
//如果你不使用ARC,记住释放patternImage
性能方面,你通常应该注意优化绘图,注意传递到 drawRect:
的rect,并确保你只绘制什么是必要的。如果你这样做,我不能看到模式绘制性能是一个主要的问题。
Is there a way to create a colored fill pattern dynamically in Cocoa?
In particular instead of using a fixed pattern from an image file via
NSColor *fillPattern = [NSColor colorWithPatternImage:patternImage];
I'd like to create a pattern by dynamically choosing the appropriate colors at runtime. Background is highlighting a colored object by rendering stripes or squares in the ''opposite'' color on top of it - whatever opposite might mean in this context, but that's a different story..
Being applied to potentially hundreds of objects in a drawing app it needs to be a rather fast method so I suppose just swapping colors in patternImage won't be good enough.
(It did work just fine back in QuickDraw..!)
Why not just draw to an in-memory image and use that for your pattern?
NSImage* patternImage = [[NSImage alloc] initWithSize:someSize];
[patternImage lockFocus];
//draw your pattern
[patternImage unlockFocus];
NSColor* patternColor = [NSColor colorWithPatternImage:patternImage];
//do something with the pattern color
//remember to release patternImage if you're not using ARC
Performance-wise, you generally should be looking at optimising drawing by paying attention to the rect passed in to drawRect:
and making sure you only draw what is necessary. If you do that then I can't see the pattern drawing performance being a major problem.
这篇关于在实时创建模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!