简体   繁体   English

适当调整iPhone5和iPad应用程序背景的图像视图的大小

[英]Properly resize Image View for iPhone5 and iPad app background

I'm using an Image View to display the background of my app, which is a single view app. 我正在使用图像视图显示我的应用程序的背景,这是一个单视图应用程序。 It's basically a calculator for iPhone5 but the problem that I have as you may know that the app should be able to run in an iPad to be able to be approved for the iTunes Store, and what is happening is that the button part of my image get cut off and some important buttons are not displayed in the screen (when its running on an iPad) 它基本上是iPhone5的计算器,但您可能知道的问题是,该应用程序应该能够在iPad上运行才能获得iTunes Store的批准,并且发生的是我图像的按钮部分被切断,并且屏幕上未显示某些重要按钮(在iPad上运行时)

I have been looking around but I can't find the proper way to achieve this and not the my image displayed with pixels or simple ugly.. 我一直在环顾四周,但找不到合适的方法来实现此目的,而不是我的图像以像素或简单的丑陋形式显示。

1. If I set the image over Xcode not programmably, its when the image get cut off (I'm doing this over my Main.storyboard and then on my attributes inspector, and setting an image that I have created in this dimensions 640 × 1136 for my iphone5 app. 1.如果我无法通过Xcode设置图像,则当图像被切断时将其设置(我正在Main.storyboard上,然后在属性检查器上进行此操作,并设置在此尺寸下创建的图像640×我的iphone5应用为1136。

2. I tried this approach too, but I had some kind of success but not what I'm looking for which is to properly show a clear background (no pixels) or (ugly) and keeping the same alignment of my buttons in the ipad display (it could be retina, mini...) 2.我也尝试过这种方法,但是我取得了某种成功,但我没有寻找能正确显示清晰背景(无像素)或(丑陋)并在ipad中保持按钮对齐的方法。显示(可能是视网膜,迷你...)

Note : to accomplish this part, I have to remove the image from my Image View and leave it blank which creates a funny ugly image.. 注意 :要完成此部分,我必须从“图像视图”中删除该图像并将其留空,以创建一个有趣的丑陋图像。

    UIGraphicsBeginImageContext(self.view.frame.size);
        [[UIImage imageNamed:@"myCal-bg-iphone5 .png"] drawInRect:self.view.bounds];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        //[self imageWithImage:thumbImage scaledToSize:CGSizeMake(30,30)]

        self.view.backgroundColor = [UIColor colorWithPatternImage:image];

3. I also tried way, changing the image of my Image View but I had not much success either.. it expands the image like crazy and in the ipad display it shows almost none buttons at all.. 3.我也尝试了一下方法,更改了“图像视图”的图像,但是我也没有获得太大的成功..它像疯了一样扩展了图像,在ipad显示器中,它几乎没有显示任何按钮。

Note : I also tried with the same image that I'm providing for the iphone5 display 注意 :我也尝试使用与iphone5显示屏相同的图像

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
        if (screenBounds.size.height == 568){
           [self.myBackground setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myCal-bg-iphone5.png"]]];
        }else{
            [self.myBackground setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myCal-bg-ipadMini.png"]]];
        }

There's a stretching property on IB. IB有伸缩性。

在此处输入图片说明

You can set an area of image which will be stretched. 您可以设置将被拉伸的图像区域。 For example, if you set X=0.9, width=0.1, the right 10% of image will be stretched with your imageView size. 例如,如果将X = 0.9设置为width = 0.1,则右边的10%的图像将使用imageView的大小进行拉伸。

Programmably? 可编程地? Use UIImage's instance method. 使用UIImage的实例方法。 resizableImageWith... resizableImageWith ...

I made it work this way, if you guys have any suggestions please let me know.. 我以这种方式使它工作,如果你们有任何建议,请让我知道。

I have a MACRO for detecting the screen size.. 我有一个用于检测屏幕尺寸的宏。

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

Then, this is in my viewDidLoad 然后,这是在我的viewDidLoad中

UIImage *image;

if ( IS_IPHONE_5 )
    image = [UIImage imageNamed:@"myCal-bg-iphone5.png"];
else
    image = [UIImage imageNamed:@"myCal-bg-iphone4.png"];


 self.myBackground = [[UIImageView alloc] initWithFrame:self.view.bounds];
 self.myBackground.image = image;
 self.myBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight;
 self.myBackground.contentMode = UIViewContentModeScaleAspectFill;
 [self.view insertSubview:self.myBackground atIndex:0];

At the end, what I'm doing is resizing the image that Im using for my iphone5 app as background to able to fit iphone4 which it also, fits in ipad but with the 1X or 2X display for iphone apps.. 最后,我正在做的是调整Im用于iphone5应用的图像的大小,使其能够适合iphone4,也可以在ipad中安装,但适用于iphone应用的1X或2X显示屏。

Note: I would love to be able to resize the whole thing.. example or in a few words, after doing this resize the buttons of my app get all in a misalignment for iphone4 or ipad becasue the screen size is different, so I'm wondering if there is a way I can control all of them and re-adjust to the new screen size/device? 注意:我希望能够重新调整整个内容的大小。例如,用几句话,调整屏幕大小后,我的应用程序按钮将全部对准iphone4或ipad,因为屏幕大小不同,所以我我想知道是否有办法可以控制所有这些设备并重新调整为新的屏幕尺寸/设备吗?

If you use Xcode5 and above, just create new image in Images.xcassets 如果您使用Xcode5及更高版本,只需在Images.xcassets中创建新图像

-Right Click Images.xcassets -> new Image Set (rename) -右键单击Images.xcassets-> new Image Set(重命名)

-Right click on work panel of your image set -> choose Rentina 4 in and iPad. -右键单击图像集的工作面板->在和iPad中选择Rentina 4。 Unselect universal image and iPhone (if not target iPhone 4) 取消选择通用图片和iPhone(如果不是针对iPhone 4)

-Drag each image associate with type -拖动与类型关联的每个图像

Then just set image name in code 然后在代码中设置图像名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM