xcode UIImage initWithContentsOfFile 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7525348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:00:36  来源:igfitidea点击:

UIImage initWithContentsOfFile doesn't work

objective-cxcodeuiimageimagenamedinitwithcontentsoffile

提问by Tanner

I have question: I want to avoid [UIImage imageNamed:]. So i did:

我有问题:我想避免 [UIImage imageNamed:]。所以我做了:

UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:@"myimage.png"]; 
controller.productImg.image = prodImg;  
[prodImg release];

But now the image is not shown anymore.

但现在图像不再显示。

Does anyone know how to get that to work?

有谁知道如何让它发挥作用?

回答by Devarshi

The above code is not working because correct way to do it is-

上面的代码不起作用,因为正确的方法是 -

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath]; 
controller.productImg.image = prodImg;  
[prodImg release];

;)

;)