Grabbing screen - kern bad access
Grabbing screen - kern bad access
- Subject: Grabbing screen - kern bad access
- From: Gábor Németh <email@hidden>
- Date: Mon, 16 Oct 2006 07:07:40 +0200
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
_______________________________________________
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