我想将在所有警报中自动显示的NSApplicationIcon图像调整为不同于应用程序捆绑中的图像。

我知道可以使用[NSApplication setApplicationIconImage:]设置停靠图标-但这仅影响停靠,而没有其他影响。

我有时可以解决此问题:我有一个NSAlert *,可以调用setIcon:来显示备用图像。

不幸的是,我想影响很多带有NSApplicationIcon和NSImageView的笔尖,创建插座并放入代码来更改图标会很麻烦。对于我要使用BeginAlert ...类型调用提出的任何警报(不会给NSAlert对象带来麻烦),我完全不走运。

任何人都可以想到一种合理的方法来全局(在正在运行的应用程序生命周期内)使用我自己的映像覆盖AppKit使用的NSApplicationIcon,以便我可以替换100%的警报(并使我的代码更简单) ?

最佳答案

困扰[NSImage imageNamed:]方法?此方法至少适用于雪豹(YMMV)。

NSImage类别中:

@implementation NSImage (Magic)

+ (void)load {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // have to call imageNamed: once prior to swizzling to avoid infinite loop
    [[NSApplication sharedApplication] applicationIconImage];

    // swizzle!
    NSError *error = nil;

    if (![NSImage jr_swizzleClassMethod:@selector(imageNamed:) withClassMethod:@selector(_sensible_imageNamed:) error:&error])
        NSLog(@"couldn't swizzle imageNamed: application icons will not update: %@", error);

    [pool release];
}


+ (id)_sensible_imageNamed:(NSString *)name {
    if ([name isEqualToString:@"NSApplicationIcon"])
        return [[NSApplication sharedApplication] applicationIconImage];

    return [self _sensible_imageNamed:name];
}

@end


有了这个技巧(未经测试,就写了)jr_swizzleClassMethod:...实现:

+ (BOOL)jr_swizzleClassMethod:(SEL)origSel_ withClassMethod:(SEL)altSel_ error:(NSError**)error_ {
#if OBJC_API_VERSION >= 2
    Method origMethod = class_getClassMethod(self, origSel_);
    if (!origMethod) {
        SetNSError(error_, @"original method %@ not found for class %@", NSStringFromSelector(origSel_), [self className]);
        return NO;
    }

    Method altMethod = class_getClassMethod(self, altSel_);
    if (!altMethod) {
        SetNSError(error_, @"alternate method %@ not found for class %@", NSStringFromSelector(altSel_), [self className]);
        return NO;
    }

    id metaClass = objc_getMetaClass(class_getName(self));

    class_addMethod(metaClass,
                    origSel_,
                    class_getMethodImplementation(metaClass, origSel_),
                    method_getTypeEncoding(origMethod));
    class_addMethod(metaClass,
                    altSel_,
                    class_getMethodImplementation(metaClass, altSel_),
                    method_getTypeEncoding(altMethod));

    method_exchangeImplementations(class_getClassMethod(self, origSel_), class_getClassMethod(self, altSel_));
    return YES;
#else
    assert(0);
    return NO;
#endif
}


然后,用这种方法说明要点:

- (void)doMagic:(id)sender {
    static int i = 0;

    i = (i+1) % 2;

    if (i)
        [[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameBonjour]];
    else
        [[NSApplication sharedApplication] setApplicationIconImage:[NSImage imageNamed:NSImageNameDotMac]];

    // any pre-populated image views have to be set to nil first, otherwise their icon won't change
    // [imageView setImage:nil];
    // [imageView setImage:[NSImage imageNamed:NSImageNameApplicationIcon]];

    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
    [alert setMessageText:@"Shazam!"];
    [alert runModal];
}


注意事项:


如上所示,任何已创建的图像视图都必须调用setImage:两次,如上所示以注册图像更改。不知道为什么。
可能比用我做的更好的方法来用imageNamed:强制初始的@"NSApplicationIcon"调用。

10-01 03:11