我有一个应用程序使用巨大的图像来适应 iPad @2x~ipad。我想在我的通用应用程序的 iPhone 版本中使用这些相同的图像。有没有办法可以为 iPhone 6+ @3x 使用这些相同的图像?
我不希望两者都具有非常相似的图像,该应用程序将在 MB 部门变得很大。
我有一个应用程序使用巨大的图像来适应 iPad @2x~ipad。我想在我的通用应用程序的 iPhone 版本中使用这些相同的图像。有没有办法可以为 iPhone 6+ @3x 使用这些相同的图像?
我不希望两者都具有非常相似的图像,该应用程序将在 MB 部门变得很大。
唯一的方法是不使用自动命名标准,而是编写代码来根据设备决定使用哪个图像名称。
假设您在按钮中使用大图像。您当前使用的代码可能看起来像这样。
[buttonView setImage:[UIImage imageNamed:largeImage] forState:UIControlStateNormal];
改为这样做。
NSString *pictFile = [[NSBundle mainBundle] pathForResource:@"largeImage" ofType:@"png"];
UIImage *imageToDisplay = [UIImage imageWithContentsOfFile:pictFile];
UIImage *checkboxImage = [UIImage imageWithCGImage:imageToDisplay.CGImage scale: [UIScreen mainScreen].scale orientation:imageToDisplay.imageOrientation];
[buttonView setImage:checkboxImage forState:UIControlStateNormal];
我有很多大图像,当视网膜显示器出现时我就开始这样做了。它同样适用于新手机。如果框架对于图像来说太大,您可能会遇到问题。例如,@3x 帧大小是 100x100,但您的图像只有 90x90。如果发生这种情况,请强制图像缩放@2x。您将无法看到照片的差异。我可以在 iPhone 6 Plus 上看到我的大型绘图有所不同,因此我将它们的比例因子保留为 2。
我正在摆脱我所有的@1x 和@2x 图像,并只使用没有@ 命名的@3x 图像。适用于按钮和背景的模拟器。
这是使用一个非常大的图像而不是三个图像的另一个示例。这次我将一个大的背景图像放入整个视图而不是一个按钮。这段代码的关键部分是,self.BGView.contentMode = UIViewContentModeScaleAspectFit; 它会调整图像的大小以使其适合视图。
- (void)pickBackgroundImage {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat scale = [UIScreen mainScreen].scale;
CGPoint midPoint = [Utilities findMidpoint:self.view];
NSString *pictFile = [[NSBundle mainBundle] pathForResource:@"Background" ofType:@"png"];
UIImage *imageToDisplay = [UIImage imageWithContentsOfFile:pictFile];
imageToDisplay = [UIImage imageWithCGImage:imageToDisplay.CGImage scale:scale orientation:imageToDisplay.imageOrientation];
CGRect pictFrame;
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat imageWidth = (unsigned int)(.9f * self.view.frame.size.width);
pictFrame = CGRectMake(midPoint.x - imageWidth/2, midPoint.y - imageWidth/2, imageWidth, imageWidth);
pictFrame.origin.y = self.view.frame.origin.y + .3f * pictFrame.origin.y;
} else {
CGFloat imageWidth = (unsigned int)(self.view.frame.size.height - 20 - 44);
pictFrame = CGRectMake(midPoint.x - imageWidth/2, midPoint.y - imageWidth/2, imageWidth, imageWidth);
pictFrame.origin.y = 10;
}
self.BGView = [[UIImageView alloc] initWithImage:imageToDisplay];
self.BGView.frame = pictFrame;
self.BGView.contentMode = UIViewContentModeScaleAspectFit;
[self.view insertSubview:self.BGView atIndex:0];
}