在iPhone 6+上使用@ 2x〜ipad
问题描述:
我有一款使用巨大图像来适应iPad @ 2x〜ipad的应用程序。我想在iPhone版的通用应用程序中使用这些相同的图像。有没有一种方法可以将这些图像用于iPhone 6 + @ 3x?在iPhone 6+上使用@ 2x〜ipad
我不希望两者都有非常相似的图像,该应用程序会在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];
}
我不明白这个 – 2014-09-29 22:24:24