Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用灰色模板图像创建像Xcode这样的彩色按钮_Objective C_Cocoa - Fatal编程技术网

Objective c 如何使用灰色模板图像创建像Xcode这样的彩色按钮

Objective c 如何使用灰色模板图像创建像Xcode这样的彩色按钮,objective-c,cocoa,Objective C,Cocoa,如果我有一个使用模板图像的NSButton,我如何像Apple在Xcode中那样对其进行着色?没有API对按钮中的模板图像进行着色。在我最新的一个应用程序中,我刚刚写了一个小工厂,它用灰色图像动态地创建彩色图像。我使用的是核心图像过滤器,它处理图像的速度非常快 这是工厂进行着色的主要方法: - (NSImage *)imageNamed:(NSString *)name withOptions:(HcToolbarImageOptions)options { NSString *key

如果我有一个使用模板图像的NSButton,我如何像Apple在Xcode中那样对其进行着色?

没有API对按钮中的模板图像进行着色。在我最新的一个应用程序中,我刚刚写了一个小工厂,它用灰色图像动态地创建彩色图像。我使用的是核心图像过滤器,它处理图像的速度非常快

这是工厂进行着色的主要方法:

- (NSImage *)imageNamed:(NSString *)name withOptions:(HcToolbarImageOptions)options
{
    NSString *key = [self keyForName:name withOptions:options];
    NSImage *image = _cache[key];
    if (!image) {
        NSImage *sourceImage = [NSImage imageNamed:name];
        NSAssert1(sourceImage != nil, @"Could not find any image named %@", name);
        if (options == 0) {
            image = sourceImage;
        } else {
            image = [NSImage imageWithSize:sourceImage.size flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
                CIContext *context = [[NSGraphicsContext currentContext] CIContext];
                CGContextRef cgContext = [[NSGraphicsContext currentContext] graphicsPort];
                NSRect proposedRect = dstRect;
                CGImageRef cgImage = [sourceImage CGImageForProposedRect:&proposedRect context:[NSGraphicsContext currentContext] hints:nil];
                CIImage *result = [CIImage imageWithCGImage:cgImage];
                if (options & HcToolbarImage_Enabled) {
                    CIFilter *filter1 = [CIFilter filterWithName:@"CIGammaAdjust"];
                    [filter1 setDefaults];
                    [filter1 setValue:result forKey:kCIInputImageKey];
                    [filter1 setValue:@0.6 forKey:@"inputPower"];
                    result = [filter1 valueForKey:kCIOutputImageKey];
                    CIFilter *filter2 = [CIFilter filterWithName:@"CIColorMonochrome"];
                    [filter2 setDefaults];
                    [filter2 setValue:result forKey:kCIInputImageKey];
                    [filter2 setValue:@1.0f forKey:kCIInputIntensityKey];
                    [filter2 setValue:[CIColor colorWithRed:0.2 green:0.5 blue:1.0] forKey:kCIInputColorKey];
                    result = [filter2 valueForKey:kCIOutputImageKey];
                }
                CGRect extent = [result extent];
                CGImageRef finalImage = [context createCGImage:result fromRect:extent];
                if (options & HcToolbarImage_NotKeyWindow) {
                    CGContextSetAlpha(cgContext, 0.5);
                }
                CGContextDrawImage(cgContext, dstRect, finalImage);
                return YES;
            }];
        }
        [_cache setObject:image forKey:key];
    }
    return image;
}

最后的图像将再次缓存在一个
NSMutableDictionary

中-如果您使用自己的工具,如可以节省大量时间


(没有,仍然没有以任何方式加入PaintCode团队。只是相信工具和自动化可以节省我们宝贵的开发时间)

我能够使用IB和您的代码添加一个带有两个内容过滤器(Gamma Adjust和Color Monology)的核心动画层,以获得我想要的结果。谢谢