Re: Saving unseen NSView with subviews to NSImage
Subject : Re: Saving unseen NSView with subviews to NSImage
From: Rob Keniger <email@hidden >
Date: Tue, 30 Dec 2008 16:22:35 +1000
Delivered-to: email@hidden
Delivered-to: email@hidden
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:message-id:from:to :in-reply-to:content-type:content-transfer-encoding:mime-version :subject:date:references:x-mailer; bh=0Qx8AxqR2p++f8/55bAR8hFB4ttjuW7Utw/JEneyZKc=; b=HRcjprmW4hq6Jhn24YlpBN5+d+3KU7TmBrPfMNiZjl143GUyK12avGqljRjLNtaW3h JtcINlAWnZeaVErISe1wem94AqRJXs3k/5kCjd1hnSsE0eXbEzYWdU+YYZb62cmDZ0T3 gYYeNFoF/Pzr9wdFrdIj+8GQpYfWFVoPstKak=
Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:message-id:from:to:in-reply-to:content-type :content-transfer-encoding:mime-version:subject:date:references :x-mailer; b=MdIJ7CNYQ2Db24jzVLoBhXM7X6WB6pJW0VXmwWdGG+OWJl7COhrUyEnlr0Id1iTrTn 67jYC9vzyBO0WRSE1GELVlpN/MBjF2+d3irL0UPrt5TO6DLtF+6uFnRLQVKiDzn+YKvH gXLFyVmkwo3nDx7s2h82PC2i6hLAzy74UsuWY=
On 30/12/2008, at 4:11 PM, John Kestner wrote:
So am I right in believing that drawRect: should not need to be
explicitly called in each subview from within my custom view's
drawRect:? In any case, see if you can make sense of this...
Seems way too complicated to me. I have this in a category on NSView,
which returns an NSImage of the view's content, including all subviews:
-(NSImage*) bitmapImageInRect:(NSRect) rect
{
NSBitmapImageRep* imageRep=[self
bitmapImageRepForCachingDisplayInRect:rect];
NSGraphicsContext *previousContext = [NSGraphicsContext
currentContext];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext
graphicsContextWithBitmapImageRep:imageRep]];
[[NSColor clearColor] set];
NSSize imageRepSize = [imageRep size];
NSRectFill(NSMakeRect(0, 0, imageRepSize.width, imageRepSize.height));
[NSGraphicsContext setCurrentContext:previousContext];
[self cacheDisplayInRect:rect toBitmapImageRep:imageRep];
NSImage* bitmapImage=[[NSImage alloc] initWithSize:rect.size];
[bitmapImage addRepresentation:imageRep];
return bitmapImage;
}
You could of course modify this to return the NSData from a -
representationUsingType:properties: call on the NSBitMapImageRep.
--
Rob Keniger
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden
This email sent to email@hidden
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.