1

我正在玩一个非常小的单视图应用程序,以了解视图是如何工作的。我对 iOS 开发非常陌生。我正在使用我放在一起的图像作为我唯一视图的背景。运行应用程序时,我的背景是巨大的。就好像它被放大或缩放一样。我正在关注 Big Nerd Ranch 视图教程,因此我正在使用 UIView 的一个子类,称为 HypnosisView。这是暂时的。值得一提的是,我尝试用作背景的图像正好是 640 x 1136 像素。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    CGRect viewFrame = CGRectMake(0, 0, 640, 1136);

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BG.png"]]];
    [[self window] addSubview:view];

    self.window.backgroundColor = [UIColor blackColor];
    [self.window makeKeyAndVisible];
    return YES;
}

更新了尝试使用 0、0、320、568 帧实现 UIImageView 的代码。我还创建了一个 320 大小的背景,然后是一个全尺寸的背景,并用正确的标题命名它们。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    CGRect viewFrame = CGRectMake(0, 0, 320, 568);


    UIImage *background = [UIImage imageNamed:@"background@2x.png"];
    UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:background] initWithFrame:viewFrame];

    [[self window] addSubview:backgroundView];
    [[self window] sendSubviewToBack:backgroundView];

    self.window.backgroundColor = [UIColor clearColor];
    [self.window makeKeyAndVisible];
    return YES;
}
4

4 回答 4

2

如果您的视图框架比您的屏幕大,您的图像也将被剪裁。您可以使用 UIScrollView,将原始图像缩小到大小或使用如下代码缩放图像:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

CGRect viewFrame = CGRectMake(0, 0, 320, 568);


UIImage *background = [UIImage imageNamed:@"BG.png"];

UIImage *scaledImage = [UIImage imageWithCGImage:[background CGImage]
                                           scale:(background.scale * 2.0)
                                     orientation:(background.imageOrientation)];

UIImageView *backgroundView = [[[UIImageView alloc] initWithImage:scaledImage] initWithFrame:viewFrame];

[[self window] addSubview:backgroundView];
[[self window] sendSubviewToBack:backgroundView];

self.window.backgroundColor = [UIColor clearColor];
[self.window makeKeyAndVisible];
return YES;
}

(我的比例因子基于 640x1136 的图像大小)

于 2013-07-09T13:57:19.197 回答
2

我猜你运行你的应用程序并且图像比窗口屏幕大,所以你认为图像实际上大于 640*1136?如果我是对的,我可以告诉你为什么。这是因为在编程中,iphone 屏幕的高度为 568,宽度为 320,而在您的代码中,您创建了一个 640*1136 的视图,它是窗口框架的两倍。所以最后它只呈现了你图像的一部分。

于 2013-07-09T01:38:28.513 回答
1

根据我的经验,如果你想通过 UIView 的 backgroundColor 设置背景图片,你需要在使用前将图片缩小到 320 x 568。这是因为 view.backgroundColor 或 [UIColor colorWithPatternImage:] 没有缩放属性。此外,最好缩小背景图像以减小应用程序大小。

如果要使用 640 x 1136 的图像,则需要使用 UIImageView 并指定其内容模式。例如,

imageView.contentMode = UIViewContentModeScaleAspectFit;

将上面的行添加到您更新的代码中,它应该可以解决您的问题。

于 2013-07-09T14:16:36.540 回答
-1

当您调用[UIImage imageNamed:@"BG.png"]iOS 时,假设您有两张图片,一张名为 BG.png,大约为 320x586,另一张名为 BG@2x.png,尺寸为 640x1136。您应该创建两个图像。

或者,您可以使用 UIImageView 并设置缩放模式。然后将该 UIImageView 添加到您的窗口。

于 2013-07-09T01:25:50.973 回答