背景图像太大在窗口/视图中

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

如果您的视图框大于屏幕,则如果图像太大,则会剪裁图像。 您可以使用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; } 

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

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

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

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

 imageView.contentMode = UIViewContentModeScaleAspectFit; 

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

当您调用[UIImage imageNamed:@"BG.png"] iOS假设您有两个图像,一个名为BG.png,大约为320×586,第二个图像名为BG@2x.png,为640×1136。 你应该创建两个图像。

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