【问题标题】:out of bound image in iOS simulatoriOS模拟器中的图像越界
【发布时间】:2013-03-10 07:16:32
【问题描述】:

我是 iPhone 开发的新手。我正在为 Crystal Ball 应用程序编写代码,当我运行 iPhone 模拟器时,其中的背景图像超出了范围。 我正在努力务实地做事。 以下是代码

导入“ViewController.h”

@interface ViewController ()

@end

@implementation ViewController
@synthesize predictionLabel;
@synthesize predictionArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImage *image = [UIImage imageNamed: @"crystal_ball copy@2x.png"];
    UIImageView *imageView =[[UIImageView alloc] initWithImage:image];
    [self. view insertSubview:imageView atIndex:0];
    
    
   self.predictionArray = [[NSArray alloc] initWithObjects:@"It is decideley so",
                                @"good things coming", 
                                @"Good time is here",
                                @"Great profit",
                                @"stay possitive",@"Stay Foccused",@"all is well",nil];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [self setPredictionLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)buttonPressed:(UIButton *)sender {
    
    NSUInteger index = arc4random_uniform(self.predictionArray. count);
    
    self.predictionLabel.text = [self.predictionArray objectAtIndex: index];
}
@end

【问题讨论】:

  • 到底是什么问题?
  • 也许您使用的是高分辨率图像(用于视网膜显示),但您运行的是错误的 iphone sim 卡?图像文件夹中是否还有 Crystal_ball copy@.png?
  • 不,我没有在图像文件夹中使用 crystal_ballcopy.png。我还需要使用什么样的图像来纠正这个问题。我想说的另一点是,当我在情节提要中使用图像视图时,相同的图像可以正常工作-但是当我尝试以编程方式执行相同操作时,问题就出现了

标签: iphone ios objective-c xcode


【解决方案1】:

您应该创建没有“@2x”的图像。 iOS 已经处理好了。

UIImage *image = [UIImage imageNamed: @"crystal_ball copy.png"];

【讨论】:

  • 试过 - 不起作用。此外,当我通过情节提要图像视图使用相同的图像时 - 一切正常 - 但如果我以编程方式尝试相同的事情 - 错误就会出现。
  • 那是因为在情节提要上,您设置了 UIImageView 的框架,并且图像被调整大小以适合框架。以编程方式,您没有设置框架,因此其大小将是图像的大小。检查图像文件尺寸是否符合您的预期。
【解决方案2】:

很高兴您在非视网膜模拟器上运行它。否则你可能没有注意到这个问题。

这是问题:

UIImage *image = [UIImage imageNamed: @"crystal_ball copy@2x.png"];

当你不知道它的历史时,视网膜的东西有点难以理解:

曾几何时,没有视网膜显示器。在这些黑暗的日子里,你的文件名只是"crystal_ball copy.png"。然后 Retina 出现了,Apple 试图让它对开发人员尽可能透明和容易。 如果您没有对代码进行任何更改,那么该应用程序仍应在视网膜设备上运行,但不会利用分辨率。如果您提供了一个双分辨率文件并将"@2x" 添加到文件名但仍然没有对您的代码进行任何更改,那么系统将自动获取视网膜设备上的高分辨率图像和非视网膜设备上的标准图像设备。

所以提供两种分辨率,一种带有"@2x",另一种没有,当提到文件名时省略"@2x"。就是这样。此行将在这里解决您的问题:

UIImage *image = [UIImage imageNamed: @"crystal_ball copy.png"];

假设此图像文件存在并且确实具有非视网膜分辨率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2015-08-19
    • 2013-01-22
    • 2021-01-05
    • 2021-02-28
    • 2019-10-23
    • 1970-01-01
    相关资源
    最近更新 更多