2012-01-10 86 views
0

好吧,所以即时尝试使它,以便当我的应用程序打开时,光标更改,但更改它即时贴图像覆盖。所以这里是我的代码...光标覆盖,Mac的目标-c

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
     // get the cursor image 
     NSPoint mouseLoc; 
     mouseLoc = [NSEvent mouseLocation]; //get cur 

     NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y); 

     // get the mouse image 
     NSImage *overlay = [[[NSCursor arrowCursor] image] copy]; 

     NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height); 

     int x = (int)mouseLoc.x; 
     int y = (int)mouseLoc.y; 
     int w = (int)[overlay size].width; 
     int h = (int)[overlay size].height; 
     int org_x = x; 
     int org_y = y; 

     size_t height = CGImageGetHeight([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]); 
     size_t width = CGImageGetWidth([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]); 
     int bytesPerRow = CGImageGetBytesPerRow([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]); 

     unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow); 

     // have the graphics context now, 
     CGRect bgBoundingBox = CGRectMake (0, 0, width,height); 

     CGContextRef context = CGBitmapContextCreate(imgData, width, 
                 height, 
                 8, // 8 bits per component 
                 bytesPerRow, 
                 CGImageGetColorSpace([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]), 
                 CGImageGetBitmapInfo([NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]])); 

     // first draw the image 
     CGContextDrawImage(context,bgBoundingBox,[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]); 

     // then mouse cursor 
     CGContextDrawImage(context,CGRectMake(0, 0, width,height),[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"arrow" ofType:@"png"]]); 

     // then mouse cursor 
     CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL]); 


     // assuming both the image has been drawn then create an Image Ref for that 

     CGImageRef pFinalImage = CGBitmapContextCreateImage(context); 

     CGContextRelease(context); 

     return pFinalImage; /* to be released by the caller */ 
    } 

因此,我试过这个,它编译与警告有关的URL路径,但它没有错误。但是,当我打开应用程序,它崩溃!所以任何人都可以帮助我?

回答

0

您正在使用NSURL*并将它传递给需要CGImageRef的函数。这甚至没有意义,所以我不知道为什么你没有得到错误。要从URL加载CGImage,您需要使用CGImageSource函数,如CGImageSourceCreateWithURL

P.S.,而不仅仅是说它崩溃,在调试器下运行它,并确切地找出它崩溃的地方。

1

如果你想改变光标,你为什么不创建一个新的实例NSCursor与你想显示的任何图像?

+0

告诉我更多信息吗?子句如何在括号中使用NSCursor查看所有括号或图像源 – 2012-01-10 21:58:01

+0

只需阅读NSCursor的文档即可。 – NSResponder 2012-01-11 10:22:14