gpt4 book ai didi

ios - 在使用绝对路径加载图像时需要一点帮助

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:18 25 4
gpt4 key购买 nike

我有 400 张具有重复名称的图像,按层次划分在我包含在我的项目中的文件夹中,现在我想使用绝对路径加载图像,我尝试使用 imagenamed 但不是图像加载,显示空框。现在我需要一些帮助,了解如何使用绝对路径显示图像。

我的项目的层次结构

Project
-> Project Files (i-e App Deligate and view controllers)
-> Resources
->Images
->1
->1.jpg
->2.jpg
->3.jpg
->2
->1.jpg
->2.jpg
->3.jpg

我使用的加载图片的方法

[self.leftImage setImage:[UIImage imageNamed:@"Images/1/1.jpg"] forState:UIControlStateNormal];

等等...任何人,帮助。问候

最佳答案

根据您的评论

下面的方法或许能帮到你

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath;
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;

- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath;
- (NSArray *)pathsForResourcesOfType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName;

你可以试试这段代码

NSString * sample=[[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"1"] ofType:@".jpg" inDirectory:@"foldername"];
[self.leftImage setImage:[UIImage imageWithContentsOfFile:sample] forState:UIControlStateNormal];

而且您不能拥有 2 张同名图片。在构建您的应用时,您会收到重复符号警告。

为了提高内存效率,请使用资源类型的路径而不是以 UIImage 命名的图像,因为它将在 ViewController 上有一些缓存内存。

希望对您有所帮助!!!

关于ios - 在使用绝对路径加载图像时需要一点帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com