NSPDFImageRep as background image

Discussion in 'Mac Programming' started by fernandovalente, May 12, 2010.

  1. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #1
    With a NSImage, I would set the window background this way:
    Code:
    [window setBackgroundColor:[NSColor colorWithPatternImage:img]]
    I have an NSPDFImageRep which I would like to set as the background color. How may I do that?

    Thanks in advanced!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Use the same NSColor method. Simply create an NSImage of the appropriate size and add the image rep you have to it.
     
  3. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #3
    All I get is a window with a black image. Also, the application crashes. I tried this:

    Code:
    NSImage *img = [[NSImage alloc] initWithSize:[rep size]];
    [img addRepresentation:rep];
    [window setBackgroundColor:[NSColor colorWithPatternImage:img]];
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Ouch! So there may be some undocumented issue using that sort of image rep then. How about you create the image, add a blank NSBitmapImageRep of the correct size etc, lock focus onto that and render the PDF rep into it?
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Can you post your compete code? If it's crashing I would imagine something else is going on.
     
  6. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #6
    Thank you guys. Found what was making my application crash.

    I still have a problem. The window background is just black, not what I want it to be.

    I'm using CGSPrivate and I would like to resize the window after the transition, and that's the only workaround I found.

    This is the entire code I'm using to set the background image(I removed everything else from the function):

    NSPDFImageRep *rep = [NSPDFImageRep imageRepWithData:[window dataWithEPSInsideRect:[window frame]]];
    NSImage *img = [[NSImage alloc] init];
    [img addRepresentation:rep];
    [window setBackgroundColor:[NSColor colorWithPatternImage:img]];
     
  7. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #7
    I found the mistake I made
    Code:
    NSPDFImageRep *rep = [NSPDFImageRep imageRepWithData:[window dataWithPDFInsideRect:[window frame]]];
    	NSImage *img = [[NSImage alloc] init];
    	[img addRepresentation:rep];
    	[window setBackgroundColor:[NSColor colorWithPatternImage:img]];
    	
    Anyway, as the screenshot doesn't have the same size of the window(as the window will be resized), the rest of the window gets black.
     

Share This Page