背景图像太大在窗口/视图中 [英] Background image too large in window/view

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

问题描述

我正在玩一个非常小的单一视图应用程序,以了解视图的工作方式。我是iOS开发的新手。我正在使用一张图片作为我唯一视图的背景。在运行应用程序时,我的背景是巨大的。就好像放大或缩放一样。我正在关注视图的Big Nerd Ranch教程,因此我使用了一个名为HypnosisView的UIView的子类。这是暂时的。我们也可能值得注意的是,我尝试用于背景的图像精确地为640 x 1136像素。

I am playing around with a very small single view application in order to understand how views work. I am extremely new to iOS development. I am using an image i put together as a background for my only view. When running the app my background is enormous. It's as if it is zoomed in or scaled. I am following a Big Nerd Ranch tutorial for views and as such I'm using a subclass of UIView called HypnosisView. This is temporary. It may also we worth noting that the image I am trying to use for a background is precisely 640 x 1136 pixels.

- (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大小的背景,然后是一个全尺寸的背景,并用适当的标题命名。

Updated code trying to implement a UIImageView using a 0, 0, 320, 568 frame. I also created a 320 sized background and then a full sized background and named them with the proper titles.

- (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;
}


推荐答案

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

If your view frame is larger than your screen, your image will be clipped if it too is bigger. You can either use a UIScrollView, shrink the original image down to size or scale your image with code like this:

- (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的图像尺寸)

(my scale factor is based on an image size of 640x1136)

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

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