如何拍摄图像并将其拉伸以填充UIView
(320/460)
我正在加载并放置它,但正如您所看到的,需要进行一些缩放。
谢谢!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:@"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
答案 0 :(得分:4)
就像我在上面对Shamsiddin的帖子中所说的那样,UIImageView需要一个大小才能知道要拉伸到哪里。在这里,我使用的是相同尺寸的UIView。我添加了另一个答案,因为缺少UIView的代码。所以这是完整的代码。
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:@"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
答案 1 :(得分:2)
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:@"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
答案 2 :(得分:0)
尝试
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
答案 3 :(得分:0)
我刚刚在我自己的iOS5应用程序中执行此操作,我需要动态设置背景图像并进行缩放。想象一下,你是在- (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:@"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
所以我们创建视图,设置它的缩放模式,添加bg图像,在主视图中将其设置为子视图,然后将其推到后面,这样它就不会占用任何其他子视图。
请接受这个答案。