在NSView中重复背景图像 [英] Repeating background image in an NSView

查看:213
本文介绍了在NSView中重复背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  // INIT $ 

我想在NSView中绘制重复的背景图片b $ b - (id)initWithFrame:(NSRect)frame {
if(self = [super initWithFrame:frame]){
self.backgroundImage = [NSImage imageNamed:@progressBackground.pdf];
}

return self;
}

// DRAW
- (void)drawRect:(NSRect)dirtyRect {
//绘制背景
[backgroundImage drawInRect:[self bounds]
fromRect:NSMakeRect(0.0f,0.0f,backgroundImage.size.width,backgroundImage.size.height)
operation:NSCompositeSourceAtop
fraction:1.0f];
NSLog(@%dx%d,backgroundImage.size.width,backgroundImage.size.height);
}

但是,视图延伸图像以填充自身。




(黑色笔画已经固定)



此外,奇怪的事情发生,因为控制台说图像的大小等于 -2109897792x0 ,但图片真的是 32x32 ! WTF?!



有人可以帮我吗?感谢。

解决方案

您可以使用+[NSColor colorWithPatternImage:] ,然后只需填充与颜色的背景矩形。这应该做你想要完成的。


I am trying to draw a repeating background image in my NSView, I have this till now:

// INIT
- (id)initWithFrame:(NSRect)frame {
  if (self = [super initWithFrame:frame]) {
    self.backgroundImage = [NSImage imageNamed:@"progressBackground.pdf"];
  }

  return self;
}

// DRAW
- (void)drawRect:(NSRect)dirtyRect {
  // Draw the background
  [backgroundImage drawInRect:[self bounds]
                     fromRect:NSMakeRect(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height)
                    operation:NSCompositeSourceAtop
                     fraction:1.0f];
  NSLog(@"%dx%d", backgroundImage.size.width, backgroundImage.size.height);
}

However, the view stretches the image to fill itself. I want the image to repeat instead.

(the black strokes are fixed already)

Also, something strange happens, as the console says the size of the image equals -2109897792x0, but the image really is 32x32! WTF?!

Could someone help me, please? Thanks.

解决方案

You can create a pattern color with +[NSColor colorWithPatternImage:] and then just fill the background rectangle with that "color". That should do what you want to accomplish.

这篇关于在NSView中重复背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆