CALayer Border, Not Inset

  • Hello,
      RIght now I am making a Core Animation program that displays photos.  This
    is the code I use to draw the image (inside the CALayer), along with a
    slightly transparent black background and a white border.

    - (void) setImage: (CGImageRef)image bounds: (CGRect)rect
    {
        self.contents = (id) image;
        self.contentsGravity = @"resize";
        self.minificationFilter = kCAFilterLinear;
        self.bounds = rect;
        CGFloat border = MAX(CGRectGetHeight(rect),CGRectGetWidth(rect));
        border /= 60;
        self.borderWidth = border;
        self.borderColor = CGColorGetConstantColor(kCGColorWhite);
        CGColorRef background =  CGColorGetConstantColor(kCGColorBlack);
        background = CGColorCreateCopyWithAlpha(background,0.8);
        self.backgroundColor = background;
    }

    The only problem is that the border is drawn over the image, because it is
    drawn inset from the bounds, and the contents of the layer go right to the
    edge.  Is there a way to either have the image draw itself a little ways out
    from the bounds (to leave room for a border) or to draw the border outset
    from the bounds?  I suppose I could make another layer within this one to
    hold the image, but being a new Cocoa programmer I am a little worried about
    making my program any more complicated than it needs to be.

      Thank You,
        Bridger Maxwell
  • On Jan 7, 2008, at 1:39 PM, Bridger Maxwell wrote:

    > Hello,
    > RIght now I am making a Core Animation program that displays
    > photos.  This
    > is the code I use to draw the image (inside the CALayer), along with a
    > slightly transparent black background and a white border.
    >
    > - (void) setImage: (CGImageRef)image bounds: (CGRect)rect
    > {
    > self.contents = (id) image;
    > self.contentsGravity = @"resize";
    > self.minificationFilter = kCAFilterLinear;
    > self.bounds = rect;
    > CGFloat border = MAX(CGRectGetHeight(rect),CGRectGetWidth(rect));
    > border /= 60;
    > self.borderWidth = border;
    > self.borderColor = CGColorGetConstantColor(kCGColorWhite);
    > CGColorRef background =  CGColorGetConstantColor(kCGColorBlack);
    > background = CGColorCreateCopyWithAlpha(background,0.8);
    > self.backgroundColor = background;
    > }
    >
    > The only problem is that the border is drawn over the image, because
    > it is
    > drawn inset from the bounds, and the contents of the layer go right
    > to the
    > edge.  Is there a way to either have the image draw itself a little
    > ways out
    > from the bounds (to leave room for a border) or to draw the border
    > outset
    > from the bounds?  I suppose I could make another layer within this
    > one to
    > hold the image, but being a new Cocoa programmer I am a little
    > worried about
    > making my program any more complicated than it needs to be.

    if you simply make the layer bounds a bit larger, and set the display
    mode so that it doesn't stretch the picture that should work.
  • Thanks Scott, but that is not quite what I am looking for.  That solution
    would normally be fine, but I need to be able to resize the CALayer with the
    picture displayed in it.  Is there a way to tell the image within it to
    resize, but not fill the whole bounds?

      Thank You,
        Bridger Maxwell

    On Jan 7, 2008 11:43 AM, Scott Anguish <scott...> wrote:

    >
    > On Jan 7, 2008, at 1:39 PM, Bridger Maxwell wrote:
    >
    >> Hello,
    >> RIght now I am making a Core Animation program that displays
    >> photos.  This
    >> is the code I use to draw the image (inside the CALayer), along with a
    >> slightly transparent black background and a white border.
    >>
    >> - (void) setImage: (CGImageRef)image bounds: (CGRect)rect
    >> {
    >> self.contents = (id) image;
    >> self.contentsGravity = @"resize";
    >> self.minificationFilter = kCAFilterLinear;
    >> self.bounds = rect;
    >> CGFloat border = MAX(CGRectGetHeight(rect),CGRectGetWidth(rect));
    >> border /= 60;
    >> self.borderWidth = border;
    >> self.borderColor = CGColorGetConstantColor(kCGColorWhite);
    >> CGColorRef background =  CGColorGetConstantColor(kCGColorBlack);
    >> background = CGColorCreateCopyWithAlpha(background,0.8);
    >> self.backgroundColor = background;
    >> }
    >>
    >> The only problem is that the border is drawn over the image, because
    >> it is
    >> drawn inset from the bounds, and the contents of the layer go right
    >> to the
    >> edge.  Is there a way to either have the image draw itself a little
    >> ways out
    >> from the bounds (to leave room for a border) or to draw the border
    >> outset
    >> from the bounds?  I suppose I could make another layer within this
    >> one to
    >> hold the image, but being a new Cocoa programmer I am a little
    >> worried about
    >> making my program any more complicated than it needs to be.
    >
    > if you simply make the layer bounds a bit larger, and set the display
    > mode so that it doesn't stretch the picture that should work.
    >
    >
    >
  • On Jan 7, 2008, at 12:17 PM, Bridger Maxwell wrote:

    > Thanks Scott, but that is not quite what I am looking for.  That
    > solution
    > would normally be fine, but I need to be able to resize the CALayer
    > with the
    > picture displayed in it.  Is there a way to tell the image within it
    > to
    > resize, but not fill the whole bounds?

    Implement a layer delegate that handles the -drawLayer:inContext:
    message, then draw the image yourself inset from the layer's edges
    enough to cover whatever you need.
    --
    David Duncan
    Apple DTS Animation and Printing
    <david.duncan...>
  • not that I can think of.

    in that case you would need to use grouped layers as I suggested..
    make the image in the inner one expand to the size layer (likely with
    respect for aspect) and keep the outer one the size you want.

    hope that makes some sense.

    On Jan 7, 2008, at 3:17 PM, Bridger Maxwell wrote:

    > Thanks Scott, but that is not quite what I am looking for.  That
    > solution
    > would normally be fine, but I need to be able to resize the CALayer
    > with the
    > picture displayed in it.  Is there a way to tell the image within it
    > to
    > resize, but not fill the whole bounds?
    >
    > Thank You,
    > Bridger Maxwell
    >
    > On Jan 7, 2008 11:43 AM, Scott Anguish <scott...> wrote:
    >
    >>
    >> On Jan 7, 2008, at 1:39 PM, Bridger Maxwell wrote:
    >>
    >>> Hello,
    >>> RIght now I am making a Core Animation program that displays
    >>> photos.  This
    >>> is the code I use to draw the image (inside the CALayer), along
    >>> with a
    >>> slightly transparent black background and a white border.
    >>>
    >>> - (void) setImage: (CGImageRef)image bounds: (CGRect)rect
    >>> {
    >>> self.contents = (id) image;
    >>> self.contentsGravity = @"resize";
    >>> self.minificationFilter = kCAFilterLinear;
    >>> self.bounds = rect;
    >>> CGFloat border = MAX(CGRectGetHeight(rect),CGRectGetWidth(rect));
    >>> border /= 60;
    >>> self.borderWidth = border;
    >>> self.borderColor = CGColorGetConstantColor(kCGColorWhite);
    >>> CGColorRef background =  CGColorGetConstantColor(kCGColorBlack);
    >>> background = CGColorCreateCopyWithAlpha(background,0.8);
    >>> self.backgroundColor = background;
    >>> }
    >>>
    >>> The only problem is that the border is drawn over the image, because
    >>> it is
    >>> drawn inset from the bounds, and the contents of the layer go right
    >>> to the
    >>> edge.  Is there a way to either have the image draw itself a little
    >>> ways out
    >>> from the bounds (to leave room for a border) or to draw the border
    >>> outset
    >>> from the bounds?  I suppose I could make another layer within this
    >>> one to
    >>> hold the image, but being a new Cocoa programmer I am a little
    >>> worried about
    >>> making my program any more complicated than it needs to be.
    >>
    >> if you simply make the layer bounds a bit larger, and set the display
    >> mode so that it doesn't stretch the picture that should work.
    >>
    >>
    >>

  • On Jan 7, 2008, at 5:03 PM, David Duncan wrote:

    > On Jan 7, 2008, at 12:17 PM, Bridger Maxwell wrote:
    >
    >> Thanks Scott, but that is not quite what I am looking for.  That
    >> solution
    >> would normally be fine, but I need to be able to resize the CALayer
    >> with the
    >> picture displayed in it.  Is there a way to tell the image within
    >> it to
    >> resize, but not fill the whole bounds?
    >
    >
    > Implement a layer delegate that handles the -drawLayer:inContext:
    > message, then draw the image yourself inset from the layer's edges
    > enough to cover whatever you need.

    Oh, well, or that.. :-)
previous month january 2008 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