Re: problem getting CALayer to draw an image
Re: problem getting CALayer to draw an image
- Subject: Re: problem getting CALayer to draw an image
- From: James Walker <email@hidden>
- Date: Thu, 05 Apr 2018 16:09:27 -0700
Sorry about the mysterious blank posts. Let's see if I can do it with
plain text rather than HTML.
On 4/5/2018 11:05 AM, David Duncan wrote:
>> On Apr 5, 2018, at 10:53 AM, James Walker <email@hidden>
wrote:
>>
>> I have a generic NSView that contains some subviews, and I'd like to
add a background image. I tried code like this:
>>
>> NSImage* backgroundImage = [NSImage imageNamed: @"blueprint
controls.png"];
>> CALayer* holderLayer = [CALayer layer];
>> _throttleHolder.layer = holderLayer;
>> _throttleHolder.wantsLayer = YES;
>> holderLayer.zPosition = 4.0f;
>> holderLayer.contents = backgroundImage;
>> holderLayer.hidden = NO;
>> holderLayer.bounds = NSRectToCGRect( _throttleHolder.bounds );
>> [holderLayer setNeedsDisplay];
> You don’t need to call -setNeedsDisplay here, and it may be what is
destroying your content (when a layer displays, its stores the result of
drawing in contents – you don’t want that since you are setting the
contents directly).
Thank you (and also thanks to Rob Petrovec). I think my first try
didn't have that line, but when it didn't work, I started making
desperate changes. In my first attempt, I tried using the layer that
the view already owned by virtue of being marked layer-backed in the
nib, rather than adding my own layer.
For the record, I get correct results with just this:
NSImage* backgroundImage = [NSImage imageNamed: @"blueprint controls.png"];
_throttleHolder.layer = [CALayer layer];
_throttleHolder.layer.contents = backgroundImage;
_______________________________________________
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