Mac下NSImage绘制模糊的原因之一是draw到了非整数像素上,框架在渲染的时候就会模糊. 针对这一原因写了以下工具:
/** * @brief 一劳永逸的解决NSImage绘制的时候绘到浮点值像素上造成的图像模糊. * @note 目前只hook了drawInRect:fromRect:operation:fraction:这个函数. * @author hUyIncHun */ #import <Cocoa/Cocoa.h> @interface NSImage(dIR) + (void)doMyWork; @end
// // NSImage+DrawSafely.m // RTX // // Created by hUyIncHun on 13-10-24. // #import "NSImage+DrawSafely.h" #import <objc/runtime.h> #import <objc/message.h> static IMP g_oldImp_drawInRect; static void newImp_drawInRect(id self, SEL _cmd, NSRect rect, NSRect rect2, NSCompositingOperation op, CGFloat f) { rect.origin.x = (int)rect.origin.x; rect.origin.y = (int)rect.origin.y; g_oldImp_drawInRect(self, _cmd, rect, rect2, op, f); } @implementation NSImage(dIR) + (void)doMyWork { static dispatch_once_t onceToken; dispatch_once(&onceToken,^ { g_oldImp_drawInRect = method_setImplementation( class_getInstanceMethod(self, @selector(drawInRect:fromRect:operation:fraction:)) , (IMP)newImp_drawInRect ); }); } @end