0

我创建一个视图如下:

// Create sub view for Logo
logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
[logoView setBackgroundColor:[UIColor blueColor]];
UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
[logoView addSubview:bgLogoView];
//NSLog(@"the button view height is %f",(screen.size.height - 230));
logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

我有一个用于 logoView 的设置框。它的高度为 230 (iphone4<) 和 318(iphone5)。我为这个块创建了一个 640x460px 的图像。

如何使此图像适合视图。目前,它将完整尺寸的图像加载到实际上只有一半大小的块中......图像被制作成两倍大小以提高质量。我正在运行 ios5 的 iphone4 上进行测试。

我确信有一两行遗漏了以某种方式使图像适合周围视图的视图边界。

4

3 回答 3

3

您需要重置图像视图的框架。正如你所拥有的,图像视图的框架将是图像的大小。另外,设置图像视图的contentMode. 最后,设置图像视图的autoresizingMask.

logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
[logoView setBackgroundColor:[UIColor blueColor]];
UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
bgLogoView.frame = logoView.bounds; // reset the frame
bgLogoView.contentMode = UIViewContentModeScaleAspectFit; // or other desired mode
bgLogoView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[logoView addSubview:bgLogoView];
//NSLog(@"the button view height is %f",(screen.size.height - 230));
logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
于 2013-03-27T21:36:28.550 回答
2

就像是

bgLogoView.contentMode = UIViewContentModeScaleAspectFit;

查看 UIView 的 contentMode 属性。可能的值是:

typedef enum {
   UIViewContentModeScaleToFill,
   UIViewContentModeScaleAspectFit,
   UIViewContentModeScaleAspectFill,
   UIViewContentModeRedraw,
   UIViewContentModeCenter,
   UIViewContentModeTop,
   UIViewContentModeBottom,
   UIViewContentModeLeft,
   UIViewContentModeRight,
   UIViewContentModeTopLeft,
   UIViewContentModeTopRight,
   UIViewContentModeBottomLeft,
   UIViewContentModeBottomRight,
} UIViewContentMode;
于 2013-03-27T21:30:52.183 回答
0

试试这个:

UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230)];
NSString *bgFilePath = [[NSBundle mainBundle] pathForResource:@"bg-welcomeLogoView"
                                                       ofType:@"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:bgFilePath];
[bgImageView setImage:img];
[logoView addSubview:bgImageView];

您可以在 initWithFrame 中更改 UIImageView 的大小:

于 2013-03-27T21:32:26.923 回答