Ios 窗口/视图中的背景图像太大

Ios 窗口/视图中的背景图像太大,ios,uiview,uicolor,Ios,Uiview,Uicolor,为了了解视图是如何工作的,我正在使用一个非常小的单视图应用程序。我对iOS开发非常陌生。我正在使用一个我放在一起的图像作为唯一视图的背景。运行应用程序时,我的背景非常丰富。就好像它被放大或缩放了。我正在遵循一个关于视图的大书呆子农场教程,因此我使用了UIView的一个子类,名为催眠视图。这是暂时的。我们可能还需要注意的是,我尝试使用的背景图像正是640x1136像素 - (BOOL)application:(UIApplication *)application didFinishLaunchi

为了了解视图是如何工作的,我正在使用一个非常小的单视图应用程序。我对iOS开发非常陌生。我正在使用一个我放在一起的图像作为唯一视图的背景。运行应用程序时,我的背景非常丰富。就好像它被放大或缩放了。我正在遵循一个关于视图的大书呆子农场教程,因此我使用了UIView的一个子类,名为催眠视图。这是暂时的。我们可能还需要注意的是,我尝试使用的背景图像正是640x1136像素

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

当您调用
[UIImage ImageName:@“BG.png”]
iOS假定您有两个图像,一个称为BG.png,大约为320x586,另一个称为BG@2x.png这将是640x1136。您应该创建两个图像


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

当您调用
[UIImageName:@“BG.png”]
iOS假定您有两个图像,一个称为BG.png,大约为320x586,另一个称为BG@2x.png这将是640x1136。您应该创建两个图像


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

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

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

如果您的视图框比屏幕大,则图像也会被剪裁。您可以使用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的图像大小)

如果您的图幅比屏幕大,则图像也会被剪裁。您可以使用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的图像大小)

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

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

imageView.contentMode = UIViewContentModeScaleAspectFit;

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

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

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

imageView.contentMode = UIViewContentModeScaleAspectFit;

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

因此,您拥有的图像是640 x 1136,但您说,当它显示在UIView中时,图像实际上大于640 x 1136?看起来是这样的。就好像我只能看到图像的左上角。iPhone 5中UIView的最大可视区域只有320 x 568。如果您的图像大于该区域,您将只能看到其中的一部分。看到我下面的答案。所以你的图像是640x1136,但是你说当它显示在你的UIView中时,图像实际上比640x1136大?看起来是这样的。就好像我只能看到图像的左上角。iPhone 5中UIView的最大可视区域只有320 x 568。如果您的图像大于该区域,您将只能看到其中的一部分。请看下面我的答案。我将视图调整为320*568,问题仍然存在。就是这样。我只需要调整我的图像大小。对我来说,让一幅图像达到所需大小的50%没有任何意义,因为我觉得它会使像素成倍增加,看起来很糟糕,但它工作得非常完美。非常感谢你@sdfrien如果您想让它显示正确的像素,请尝试创建两张图片,一张是320*568,另一张是640*1136。并将它们命名为xxxxx.png和xxxxx@2x.png. 然后把两张照片都放在你的项目里。当你的照片显示在视网膜屏幕上时,系统会自动为你选择640*1136。我将我的视图调整为320*568,问题仍然存在。就是这样。我只需要调整我的图像大小。对我来说,让一幅图像达到所需大小的50%没有任何意义,因为我觉得它会使像素成倍增加,看起来很糟糕,但它工作得非常完美。非常感谢你@sdfrien如果您想让它显示正确的像素,请尝试创建两张图片,一张是320*568,另一张是640*1136。并将它们命名为xxxxx.png和xxxxx@2x.png. 然后把两张照片都放在你的项目里。当你的图片显示在视网膜屏幕上时,系统会自动为你选择640*1136的图片