5

我正在从网络 Apple.png 和 Apple@2x.png 下载两个图像。我想使用[UIImage imageNamed:@"Apple.png"]它,以便它可以使用内置功能来检测它是否应该显示 Apple.png 或 Apple@2x.png。

现在我在哪里存储这些图像?我在文档中阅读了以下内容:

文件的名称。如果这是第一次加载图像,该方法会在应用程序的主包中查找具有指定名称的图像。

啊,所以应用程序的主包是要走的路。这就是我的代码的样子:

NSString      *directory   = [[NSBundle mainBundle] bundlePath]; 
NSString      *path        = [directory stringByAppendingPathComponent:imageName];
NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager createFileAtPath:path contents:dataFetcher.receivedData attributes:nil];

我检查了文件是否在路径的文件夹中创建并且是正确的。我还在我的项目文件中拖动了一个 Example.png,只是为了查看它是否存储在同一个文件夹中,这也是正确的。

但是,[UIImage imageNamed:@"Apple.png"]仍然无法获取图像。

4

6 回答 6

2

您不能对应用程序下载的图片使用UIImage'a+imageNamed:方法。该方法确实在应用程序包中查找图像,但您的应用程序不允许在运行时更改自己的包。

于 2011-01-21T01:34:01.683 回答
1

将图像(当然是异步的)下载到应用程序本地的商店(即沙箱),并在本地引用它们。然后,您可以使用NSDocumentDirectoryNSCachesDirectory始终获取对其位置的引用。

于 2011-01-21T01:35:02.903 回答
0

创建的文件是否具有适当的权限以供您访问?你确定保存后关闭文件吗?

于 2011-01-21T01:33:25.383 回答
0

你不能在运行时修改你的包。我还认为这imageNamed使用了在包含捆绑资源引用的应用程序中初始化的资源映射之类的东西。

试试这个:http: //atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/

再见!

于 2011-01-21T01:46:09.533 回答
0

我认为最好的解决方案是在 Library 文件夹中制作您自己的捆绑包,例如images.bundle. 在那里,您可以存储两种分辨率的图像。要访问该图像,您可以使用 NSBundle 类的 pathForResource:ofType: 方法。它返回适当的图像。比具有返回路径的初始化图像。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSURL *libraryURL = [fileManager URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
if (libraryURL == nil) {
    ALog(@"Could not access Library directory\n%@", [error localizedDescription]);
} else {
    NSURL *folderURL = [libraryURL URLByAppendingPathComponent:@"images.bundle"];
    DLog(@"Future bundle url = %@",folderURL);
    if (![fileManager createDirectoryAtURL:folderURL withIntermediateDirectories:YES attributes:nil error:&error]) {
        ALog(@"Could not create directory %@\n%@", [folderURL path], [error localizedDescription]);
    } else{
        NSBundle *bundle = [NSBundle bundleWithURL:folderURL];
        // put image and image@2x to the bundle;
        [bundle pathForResource:yourImageName ofType:@"png"];
    }
}
于 2012-09-18T19:09:15.573 回答
0

OP的解决方案。

UIImage 的类别:

@implementation UIImage (ImageNamedExtension)

+ (UIImage *)imageNamed:(NSString *)name fromDirectory:(NSString *)directory {

    if ([UIScreen mainScreen].scale >= 3.0) {
        NSString *path3x = [directory stringByAppendingPathComponent:[[name stringByDeletingPathExtension] stringByAppendingFormat:@"@3x.%@", name.pathExtension]];
        UIImage *image = [UIImage imageWithContentsOfFile:path3x];
        if (image) {
            return image;
        }
    }
    if ([UIScreen mainScreen].scale >= 2.0) {
        NSString *path2x = [directory stringByAppendingPathComponent:[[name stringByDeletingPathExtension] stringByAppendingFormat:@"@2x.%@", name.pathExtension]];
        UIImage *image = [UIImage imageWithContentsOfFile:path2x];
        if (image) {
            return image;
        }
    }
    NSString *path = [directory stringByAppendingPathComponent:name];
    return [UIImage imageWithContentsOfFile:path];
}

@end
于 2018-03-12T01:50:42.123 回答