Grabbing screen - kern bad access

  • Hello,

    I am a beginner in the field of Cocoa, so please forgive me if this
    is a RTFM style question, however i've did a good amount of googling
    before writing here, so even a pointer to the appropriate place in
    the manual is appreciated.
    I've defined a simple form with an NSImageView and an NSButton. When
    the button is pressed the program should grab the contents of the
    screen, and place it in the NSImageView.  For further processing, the
    NSImageview is just a POC control.

    The problem is: If i step through the code with the debugger, or have
    quartz debug active it runs well. If not, the program dies with
    kern_bad_access.
    It seems that initWithFocusedViewRect can not complete because some
    un-flushed data.
    Any idea what should I flush? or what should I do, in order to avoid
    the crash?

    Here is the code :

    - (IBAction)doItNow:(id)sender{
      screenRect = [[NSScreen mainScreen] frame];
      NSImage *screenImg = [[NSImage alloc] initWithSize:NSMakeSize
    (screenRect.size.width, screenRect.size.height)];
      [screenImg setScalesWhenResized:YES];
      window = [[NSWindow alloc] initWithContentRect:screenRect

    styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained
                                                          defer:NO];
      myView = [[NSView alloc] initWithFrame:screenRect];
      [window setLevel:NSScreenSaverWindowLevel + 100];
      [window setHasShadow:NO];
      [window setAlphaValue:0.0];
      [window setContentView:myView];
      [window orderFront:self];
      float scaleFactor = 0.5;
      [myView lockFocus];
      NSBitmapImageRep *screenRep= [[NSBitmapImageRep alloc]
    initWithFocusedViewRect:screenRect];
      [screenImg addRepresentation:screenRep];

      [myView unlockFocus];
      NSGraphicsContext *gctxt = [NSGraphicsContext currentContext];
      [gctxt flushGraphics];

      [window flushWindow];
      [window orderOut:self];
      [window close];

      NSImage *scaledImage = [[NSImage alloc] initWithSize:NSMakeSize
    ([screenImg size].width * scaleFactor, [screenImg size].height *
    scaleFactor)];
      [scaledImage lockFocus];
      [screenImg drawInRect:NSMakeRect(0,0,[scaledImage size].width,
    [scaledImage size].height)
                    fromRect:NSMakeRect(0,0,[screenImg size].width,
    [screenImg size].height)
                  operation:NSCompositeCopy fraction:1.0];
      [scaledImage unlockFocus];

      [imageBox setImage:scaledImage];
    }

    Thanks in advance,
      Gabor
  • On 15 Oct 06, at 22:07, Gábor Németh wrote:
    > I am a beginner in the field of Cocoa, so please forgive me if this
    > is a RTFM style question, however i've did a good amount of
    > googling before writing here, so even a pointer to the appropriate
    > place in the manual is appreciated.
    > I've defined a simple form with an NSImageView and an NSButton.
    > When the button is pressed the program should grab the contents of
    > the screen, and place it in the NSImageView.  For further
    > processing, the NSImageview is just a POC control.

    There's a better way to do the grab. It's not documented, so it's a
    little bit magic, but it's how Grab.app works, so it should work fine
    for the time being. I've posted about it previously - see http://
    www.cocoabuilder.com/archive/message/cocoa/2006/7/20/167950 for some
    sample code. _______________________________________________
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list      (<Cocoa-dev...>)
    Help/Unsubscribe/Update your Subscription:
    http://lists.apple.com/mailman/options/cocoa-dev/<cocoa...>

    This email sent to <cocoa...>
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today