- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经多次尝试将使用 colorWithPatternImage 的图像创建的 NSColor 来回转换为 CGColor。它总是失败,我得到奇怪的坏结果。
我想知道转换是否适用于使用平铺图像生成的颜色,或者根本不可能。这就是我转换颜色的方法:
-(CGColorRef) CIColorToCGColor: (CIColor *) ciColor {
CGColorSpaceRef colorSpace = [ciColor colorSpace];
const CGFloat *components = [ciColor components];
CGColorRef cgColor = CGColorCreate (colorSpace, components);
CGColorSpaceRelease(colorSpace);
return cgColor;
}
...
CIColor *ciColor = [[CIColor alloc] initWithColor: [NSColor colorWithPatternImage:backgroundImage]];
CGColorRef cgColor = [self CIColorToCGColor: ciColor];
[ciColor release];
最佳答案
几周前我需要这样做。我发现这个答案基本上解决了问题, How to tile the contents of a CALayer?
这是更方便的形式,
// callback for CreateImagePattern.
static void DrawPatternImage (void *info, CGContextRef ctx) {
CGImageRef image = (CGImageRef) info;
CGContextDrawImage(ctx,
CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)),
image);
}
// callback for CreateImagePattern.
static void ReleasePatternImage( void *info ) {
CGImageRelease((CGImageRef)info);
}
CGColorRef CreateCGColorWithPatternFromNSImage( NSImage *image ) {
/* User needs to release the color */
NSData *imageData = [image TIFFRepresentation];
CFDataRef cfImageData = CFBridgingRetain(imageData);
CGImageSourceRef source = CGImageSourceCreateWithData(cfImageData, NULL);
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(cfImageData);
CFRelease(source);
//assume image is the CGImage you want to assign as the layer background
CGFloat width = [image size].width;
CGFloat height = [image size].height;
static const CGPatternCallbacks callbacks = {0, &DrawPatternImage, &ReleasePatternImage};
CGPatternRef pattern = CGPatternCreate (cgImage,
CGRectMake (0, 0, width, height),
CGAffineTransformMake (1, 0, 0, 1, 0, 0),
width,
height,
kCGPatternTilingConstantSpacing,
true,
&callbacks);
CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
CGFloat components[1] = {1.0};
CGColorRef color = CGColorCreateWithPattern(space, pattern, components);
CGColorSpaceRelease(space);
CGPatternRelease(pattern);
return color;
}
关于objective-c - 将使用 colorWithPatternImage 创建的 NSColor 转换为 CGColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892550/
是否有 NSColor 的“免费桥接”Core Foundation 对应版本? CGColorRef 似乎不喜欢 NSColor ,反之亦然? 基本上,我们希望从存储实际 r/g/b/a 值的 C+
我扩展 NSColor 以添加类方法 + (NSColor*) initWithHTMLString: (NSString*) rgb 我设置 NSWindow 对象的背景颜色: [self.wind
我正在尝试将 NSColor 的实例保存到如下文件中: writeF(node.lineColour.hueComponent) writeF(node.lineColour.saturati
我正在使用 sip 开发者工具从此图像中获取特定颜色: http://dribbble.com/shots/1277834-Login-Thumb/attachments/176317 (右侧底部灰色
在我的 View 背景中,我绘制了浅蓝色。在中间,我有一个方形盒子,里面应该有更浅的灰色,透明度为 20%。但由于某种原因,透明度位于黑色背景而不是蓝色背景之上。如果我没说清楚,我很抱歉。 最佳答案
我正在尝试在 NSView 中绘制内部阴影。阴影本身不是问题,但颜色设置让我发疯:/ #define ShadowBlurRadius 10.0 #define SRGB (CGFloat [4]){
我不太明白为什么 NSColor 的色调组件会表现得像它那样。这里有一些奇怪的事情: NSColor *c = [NSColor colorWithCalibratedHue:0.1
我知道 Cocoa 为您提供了 whiteColor、blackColor、darkGrayColor,但是它们也有 Apple 颜色面板中的颜色吗?有“雪”、“钨”、“钢”、“锡”等颜色?或者我应该
我正在实现一个自定义 NSMenuItem View shows a highlight当用户将鼠标悬停在其上时。为此,代码在将 [NSColor selectedMenuItemColor] 设置为
我正在寻找一种反转任意 NSColor 的方法在运行时更改值,并且似乎没有任何内置方法可以执行此操作。 我将使用类别来扩展 NSColor,如下所示: NSColor * invertedColor
我可以在Xcode Assets 管理中创建这样的颜色,当然需要10.13以上才能应用。 我目前正在向我的应用程序添加主题功能,我认为编写一个自动适应明亮或黑暗模式的 NSColor 对象可能会有所帮
我需要保证颜色有足够的饱和度。 我对 NSColor 做了这个扩展,但我的测试给了我奇怪的值:tempColor 似乎总是有很多饱和度。 所以基本上我的函数总是返回 self? 我的错误在哪里? 我也
我创建了 NSWindow 并将其背景色设置为绝对蓝色 (#0000FF)。但是当窗口被渲染时,颜色比它应该的颜色“更亮”(#0F3FFB)。 class LilWindow: NSViewContr
我在将十六进制代码转换为 NSColor 时遇到了一些问题。请注意,这是针对 Mac 应用程序的(因此使用 NSColor 而不是 UIColor)。这是我到目前为止的代码: - (NSColor *
我正在尝试通过在 NSViewController 中切换暗/亮模式来更改图像的颜色。 我正在使用此代码更改图像的颜色: - (NSImage *)image:(NSImage *)image wit
好吧,长话短说: 我正在使用(嵌入到 bundle 中)FontAwesome 我将它用作一些自定义 NSButton 中的字体 在 NSButton 子类中,我想为它们着色,就像 Xcode 选项卡
我正在使用此代码: #define MAATTACHEDWINDOW_DEFAULT_BACKGROUND_COLOR [NSColor colorWithCalibratedRed:59 green
我正在学习教程,下面的方法属于 NSview 的子类。它只是绘制一个红色矩形。我只是不明白它是如何做到的。我不明白设置消息在 [bkgColor set] 中的作用以及它如何影响背景颜色? - (vo
NSAnimation 不允许我对 NSColor 更改进行动画处理。我该怎么做? 最佳答案 您可以使用blendedColorWithFraction:ofColor:。在您的动画方法中(无论哪种方
是否可以将 NSGradient 转换为 NSColor - (void) viewWillDraw { NSGradient *grad = [[NSGradient alloc] init
我是一名优秀的程序员,十分优秀!