Re: Saving unseen NSView with subviews to NSImage
Re: Saving unseen NSView with subviews to NSImage
- Subject: Re: Saving unseen NSView with subviews to NSImage
- From: "Michael Ash" <email@hidden>
- Date: Tue, 30 Dec 2008 19:19:29 -0500
On Tue, Dec 30, 2008 at 1:50 PM, John Kestner <email@hidden> wrote:
>
> On Dec 30, 2008, at 8:22 AM, Michael Ash wrote:
>
>> You make this brand-new view, and never put any subviews into it. It
>> should be pretty well obvious why no subviews ever show up....
>
> Yeah, it started to dawn on me last night when NSLog(@"%@", [self subviews])
> turned up no subviews. I have a disconnect between what thawing view objects
> from a nib and programmatically creating those objects does. Is the correct
> course of action to programmatically instantiate and add subviews? Is there
> a way to avoid updating code every time I change the layout in IB? Or is
> this a case where I'm better off drawing strings in my view's drawRect:?
You're thinking about this too hard.
If you want to load your view from the nib, write code to load the nib.
If you want to create a view in code, write code to create the view.
You want to load the view from the nib, but you wrote code to create
the view. This does not make sense.
Load the nib, get the view from the nib, and use that.
>>> [self drawRect:pRect];
>>
>> Do not do this. It will not work. -drawRect: does not magically draw
>> all subviews too. Of course you have no subviews so it won't work even
>> then, but this second piece is going to prevent correct behavior even
>> after you correct the first piece. Use -dataWithPDFInRect: instead.
>> You mentioned using this in your first e-mail, and it's the right
>> thing to do. It *will* capture all subviews as well. It wasn't working
>> for you, presumably, because you had no subviews to capture.
>
> Iin the code I provided, I do need to call that drawRect: to get at least
> the custom view to draw.
> I need to save the images as JPEG, so dataWithPDFInRect doesn't work for me.
Well no, you do not need to call -drawRect:. Let me say this again,
plainly: it *will not work*. It is never necessary to do something
which doesn't work, by definition.
-dataWithPDFInRect: will work fine. You can create a PDF, then draw
that into a bitmap context, then create a JPEG from that bitmap
context.
You can also use the more direct -cacheDisplayInRect:toBitmapImageRep:
call. However it will produce a bitmap with subpixel antialiasing
depending on the user's system preferences which is *usually* not
desirable. The PDF approach will give you consistent results with no
subpixel stuff.
Mike
_______________________________________________
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:
This email sent to email@hidden