2011-01-21 67 views
5

我從網絡Apple.png和[email protected]下載兩個圖像。我想使用[UIImage imageNamed:@「Apple.png」],因此它可以使用內置功能來檢測它是否應顯示Apple.png或[email protected](iPhone)在UIImage中使用外部圖像imageNamed

現在我在哪裏存儲這些圖像?我讀的文檔中的以下內容:

文件的名稱。如果這是第一次加載圖像 ,則該方法將在應用程序的主包中查找 指定名稱的圖像。

阿讓應用程序的主束是要走的路。這是我的代碼看起來像:

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」]仍然無法獲取圖像。有人有想法嗎?


編輯:

我爲UIImage的一個類別:

@implementation UIImage的(ImageNamedExtension)

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

    NSString  *name2x  = [name stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@".%@", [name pathExtension]] withString:[NSString stringWithFormat:@"@2x.%@", [name pathExtension]]]; 
    NSString  *path  = [directory stringByAppendingPathComponent:name]; 
    NSString  *path2x  = [directory stringByAppendingPathComponent:name2x]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) { 

     if ([fileManager fileExistsAtPath:path2x]) { 
      return [UIImage imageWithContentsOfFile:path2x]; 

     } 

    } 

    return ([fileManager fileExistsAtPath:path]) ? [UIImage imageWithContentsOfFile:path] : nil; 
} 

@end 

這eventualy的伎倆。感謝所有的幫助。

+0

+1代碼格式。 – WrightsCS 2011-01-21 01:34:20

+0

您無法在iPhone上的主包中創建文件。模擬器可以讓你這樣做,但設備不會/不應該。如果你正在下載,你必須把它放在別的地方。 – 2011-01-21 01:35:07

+0

如果僅實現了@ 2x,則您的類別將停止在iPad(第一代)上(即,也需要1x)。在相同的情況下也可能在iPhone 3GS上打破。 – Jonny 2012-11-05 04:28:25

回答

0

正在創建的文件是否具有適當的權限供您訪問?保存後是否確定關閉了文件?

2

您不能使用UIImage'a +imageNamed:方法與應用程序下載的圖片。該方法確實在應用程序包中查找圖像,但您的應用程序不允許在運行時更改自己的包。

1

將圖像(當然是異步的)下載到您的應用程序本地存儲(即您的沙箱)中,並在本地引用它們。然後,您可以使用NSDocumentDirectoryNSCachesDirectory來始終獲取對其位置的引用。

0

我認爲最好的解決方案是在庫文件夾中創建自己的包,例如images.bundle。您可以在這兩種分辨率下劃分圖像。要訪問這些圖像,可以使用NSBundle類的pathForResource:ofType:方法。它會返回適當的圖像。帶有返回路徑的init圖像。

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 [email protected] to the bundle; 
     [bundle pathForResource:yourImageName ofType:@"png"]; 
    } 
} 
相關問題