Setting custom view background image via NSImageView

  • Hello all!

    I am trying to make my custom view have a background image. What I
    did is:

    1. Add the UIbackground.png image to the project and the interface
    builder (shows as UIbackground in the "Images" tab of the builder)

    2. Select all GUI elements, Layout -> make subview of -> custom view,
    set the custom class of this custom view to NSImageView

    3. Define a connection

    @interface SinevibesTransformerView : NSView
    {
      IBOutlet NSImageView *    uiView;
    ...
    }

    4. In the interface builder, connect the NSImageView custom view to
    uiView

    5. Then in @implementation of my view:

    - (void)awakeFromNib
    {
    [uiView setImage: [NSImage imageNamed: @"UIbackground"]];
    }

    Is this the correct way? Asking this because it compiles fine but
    doesn't work ;-)

    Thanks for any hints,

    Artemiy.
  • You should first override your |drawRect:| method of your NSView subclass.

    Then you can use the following method:

    - (void)drawTiledInRect:(NSRect)rect origin:(NSPoint)inOrigin
    operation:(NSCompositingOperation)inOperation
    {
      NSGraphicsContext* gc = [NSGraphicsContext currentContext];
      [gc saveGraphicsState];

      [gc setPatternPhase:inOrigin];

      NSColor* patternColor = [NSColor colorWithPatternImage:self];
      [patternColor set];
      NSRectFillUsingOperation(rect, inOperation);

      [gc restoreGraphicsState];
    }

    ... To draw the background like this:
    NSPoint patternOrigin = [self convertPoint:NSMakePoint(0.0f, 0.0f) toView:nil];
    [yourNSImageItem drawTiledInRect:aRect origin:patternOrigin
    operation:NSCompositeCopy];

    On 9/29/06, Artemiy Pavlov <artemiy.pavlov...> wrote:
    > Hello all!
    >
    > I am trying to make my custom view have a background image. What I
    > did is:
    >
    > 1. Add the UIbackground.png image to the project and the interface
    > builder (shows as UIbackground in the "Images" tab of the builder)
    >
    > 2. Select all GUI elements, Layout -> make subview of -> custom view,
    > set the custom class of this custom view to NSImageView
    >
    > 3. Define a connection
    >
    > @interface SinevibesTransformerView : NSView
    > {
    > IBOutlet NSImageView *                          uiView;
    > ...
    > }
    >
    > 4. In the interface builder, connect the NSImageView custom view to
    > uiView
    >
    > 5. Then in @implementation of my view:
    >
    > - (void)awakeFromNib
    > {
    > [uiView setImage: [NSImage imageNamed: @"UIbackground"]];
    > }
    >
    > Is this the correct way? Asking this because it compiles fine but
    > doesn't work ;-)
    >
    >
    > Thanks for any hints,
    >
    > Artemiy.
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<kreeger.dev...>
    >
    > This email sent to <kreeger.dev...>
    >
previous month september 2006 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  
Go to today