簡體   English   中英

更改圖像時,UIImageView的圖像消失

[英]UIImageView's image disappears when changing the image

在代碼中更改UIImageView上的圖像時,該圖像消失。 這僅在iPod Touch設備上發生,而不在iPhone仿真器上發生。

在我的捆綁包中,有一個名為SomeImage.PNG的圖像

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"png"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];

我必須重申,這在iPhone模擬器上可以正常運行,但在iPod Touch設備上不能正常運行。

請注意,圖像文件的文件擴展名為大寫,但在我的代碼中聲明為小寫。 這就是問題。 因此,要解決此問題,請將代碼更改為此:

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeImage" ofType:@"PNG"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];

我在問是否有一種“更好”的方式來做我正在做的事情,所以將來我不會遇到此類問題。

並不是的。 不幸的是,iOS文件系統區分大小寫。

像這樣.....?

NSString *filename = @"SomeImage";
NSString *extension = @"png";
UIImage *img = nil;
NSString *path = nil;

path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension lowercaseString]];
img = [[UIImage alloc] initWithContentsOfFile:path];

if (img == nil) {
    path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension uppercaseString]];
    img = [[UIImage alloc] initWithContentsOfFile:path];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM