带有setImage的UIImageView:图像在调试器设备上不可见

时间:2012-09-10 00:01:43

标签: iphone objective-c uiimageview

我正在尝试使用以下方法显示图像:

[myImageView setImage:[UIImage imageNamed:@"myImage.png"]];

它与iPhone模拟器配合得很好。但是,当我在设备上进行调试时,图像不可见。

显然它是关于分辨率的,因为我的图像的分辨率远远高于UIImageView(并且它必须是...),而且当我使用具有相同UIImageView分辨率的图像时,它在设备上也能正常工作。

我尝试了here所说的话,但它并没有解决我的问题。

你有什么建议让UIImageView使用比它的框架更大的图像?

提前致谢。

4 个答案:

答案 0 :(得分:0)

如果图像分辨率是您的问题,请尝试提供的解决方案here。否则,请尝试Apple参考库中的示例PhotoScroller。它提供了一种以块(tile)显示大图像的方法。那应该做你想要的。

答案 1 :(得分:0)

这样做,

myImageView=[[UIImageView alloc]initWithFrame:CGRectMake(50,50,200,300)];
[myImageView setImage:[UIImage imageNamed:@"myImage.png"]];
[self.view addSubView:myImageView];

它会起作用。

答案 2 :(得分:0)

这是有时可能发生的最简单的问题。这是因为当模拟器忽略图像名称的区分大小写时,设备需要图像名称区分大小写。

因此,请确保您在bungle中的图像名称与此处指定的完全相同。甚至没有资本或小写字母差异。只需检查图像名称 - “myImage.png”

如果您在UIImageView图像显示中仍有任何问题,请告诉我。

答案 3 :(得分:0)

由于我正在下载要在运行时显示的图像,因此我将使用以下代码获取应用程序主包路径:

NSString *path = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] executablePath]];
NSArray *parts = [path componentsSeparatedByString:@"/"];
NSString *pathToSave = [path substringToIndex:([path length] - [[NSString stringWithFormat:@"%@", [parts objectAtIndex:[parts count]-1]] length])];

我使用以下代码保存图像:

NSString *url = [NSString stringWithFormat:@"%@", [imageURLs objectAtIndex:0]];
NSString *filename = [url lastPathComponent];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *localFilePath = [NSString stringWithFormat:@"%@%i%@", pathToSave, [[NSString stringWithFormat:@"%@", [element child:@"id"]] intValue], filename];
[thedata writeToFile:localFilePath atomically:YES];

当我手动下载图像并添加到项目时,[UIImageView setImage:]这次确实有效。所以我意识到从URL保存图像有问题。

我不认为这应该是问题的原因是它与iOS模拟器一起工作得很好。

我将看看这个问题,但是因为我的应用程序名称包含特殊字符,如“İ”(土耳其语),我认为可能是原因,应用程序可能无法保存到正确的路径。