• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Not even a portion of a window?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Not even a portion of a window?


  • Subject: Re: Not even a portion of a window?
  • From: Troy Stephens <email@hidden>
  • Date: Fri, 15 Jul 2005 11:16:23 -0700

On Jul 15, 2005, at 10:20 AM, Lorenzo wrote:
H,
why can't I get an image of my window?
I get an empty image all the time. What did I miss?

    NSRect        imageRect = [[aWindow contentView] bounds];
    NSSize        imageSize = imageRect.size;
    NSData        *imageData = nil;

    NSImage *image = [[NSImage alloc] initWithSize:imageSize];
    [image lockFocus];
        [[aWindow contentView] drawRect:imageRect];
        imageData = [image TIFFRepresentation];
    [image unlockFocus];

    [imageData writeToFile:@"/a_image_%d.tiff" atomically:YES];
    [image release];

Best Regards
-- Lorenzo
email: email@hidden

-drawRect: relies on some setup that AppKit provides when invoking it. With the exception of sending [super drawRect:aRect] from a subclass' implementation of this method, it isn't meant to be invoked directly by apps. Also, it would only result in drawing the contentView in this case (which by default is a simple NSView that draws nothing); -drawRect: itself does not recurse to draw subviews and descendants.


If you want a snapshot of the content portion of your window, the recommended technique is to use NSBitmapImageRep's - initWithFocusedViewRect: method:

NSView *contentView = [aWindow contentView];
NSRect imageRect = [contentView bounds];
[contentView lockFocus];
NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];
[conventView unlockFocus];


This will snapshot the window's content area, including the background fill. On Tiger, there is also some new NSView API that enables you to render a view subtree to a bitmap (which omits the background that would otherwise be provided by the window and any ancestor views):

- (void)displayRectIgnoringOpacity:(NSRect)aRect inContext: (NSGraphicsContext *)context;
- (NSBitmapImageRep *)bitmapImageRepForCachingDisplayInRect:(NSRect) rect;
- (void)cacheDisplayInRect:(NSRect)rect toBitmapImageRep: (NSBitmapImageRep *)bitmapImageRep;


--
Troy Stephens
Cocoa Frameworks
Apple Computer, Inc.


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Not even a portion of a window?
      • From: Lorenzo <email@hidden>
References: 
 >Not even a portion of a window? (From: Lorenzo <email@hidden>)

  • Prev by Date: Re: Path of a folder represented in AppleScript path style
  • Next by Date: Preventing flicker on moving NSView to a different superview
  • Previous by thread: Re: Not even a portion of a window?
  • Next by thread: Re: Not even a portion of a window?
  • Index(es):
    • Date
    • Thread