gpt4 book ai didi

ios - 从支持文件中的图像目录获取图像路径

转载 作者:行者123 更新时间:2023-11-28 19:08:29 24 4
gpt4 key购买 nike

我在 Xcode 的 Supported Files 目录下的 images 目录中存储了一堆图像。我希望能够展示其中一张图片。获取该图像路径的最佳方法是什么?我必须先将它们复制到 Documents 目录吗?如果可以,我该怎么做?

编辑:我尝试了以下方法将图像从支持文件复制到应用程序中的文档文件夹。它复制成功,但我无法显示图像:

-(void)findImage:(NSString *)imageName
{

// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",imageName]];

success = [fileManager fileExistsAtPath:appImagePath];
if (success)
{
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultImagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",imageName]];
success = [fileManager copyItemAtPath:defaultImagePath toPath:appImagePath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
self.imageDisplay.image = [UIImage imageNamed:appImagePath];
return;
}

最佳答案

这应该可以解决问题:

[UIImage imageNamed:@"someImageName"];

编辑:一些附加信息:-imageNamed:将在应用程序的整个主包中查找文件名为“someImageName”的图像文件(最好是 png)。您不必担心它的位置或扩展名,因为它会在 mainbundle 中搜索。您通过 xcode 中的 import-file-dialogue 导入的文件将被添加到他的主包中。

这意味着:如果我导入了一个名为 myImage.png 的文件,在我的代码中的任何地方调用 [UIImage imageNamed:@"myImage"]; 都会得到一个包含该图像的 UIImage-Object。它非常简单,也许这让您有点吃惊 ;)

如果你愿意,可以在文档中查找: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html

关于ios - 从支持文件中的图像目录获取图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18063864/

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