我有一个应用程序,使用巨大的图像来适应iPad @ 2x~ipad。我想在我的通用应用程序的iPhone版本中使用相同的图像。有没有办法可以在iPhone 6+ @ 3x上使用相同的图像?
我不希望两者的图像非常相似,应用程序将在MB部门变大。
答案 0 :(得分:0)
唯一的方法是不使用自动命名标准并编写代码,根据设备决定使用哪个图像名称。
答案 1 :(得分:0)
假设您在按钮中使用大图像。您当前使用的代码可能看起来像这样。
[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图像。适用于按钮和背景的模拟器。
答案 2 :(得分:0)
这是另一个使用一个非常大的图像而不是三个图像的例子。这次我将大背景图像放入整个视图而不是按钮。这段代码的关键部分是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];
}