2017-07-14 115 views
0

我已經寫代碼學習的答案在這裏發現了問題之後打開圖像文件(abcdef & g)。但即使我將png文件添加到項目中,NSFileManager也無法找到它們。我相當有信心我的代碼應該能夠識別任何一個png文件,如果它們在正確的目錄中。爲什麼NSFileManager無法找到這些png文件?

e.g. 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSFileManager *fileManager = [[NSFileManager alloc] init]; 

    NSArray *dirPaths; 
    NSString *docsDir; 

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

    NSLog(@"\ndirPaths %@ \n\ndocsDir \n%@", dirPaths, docsDir); 

    NSString *imageFilePath = [docsDir stringByAppendingPathComponent:@"iTunesArtwork1024.png"]; 

    NSLog(@"\n\nfile path should be \n\n%@ \n\n", imageFilePath); 

    NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath]; 

    if ([fileManager fileExistsAtPath:imageFilePath]) 

    { 
     NSLog(@"\nFound file path : %@", imageFilePath); 
    } 

    else 

    { 
     NSLog(@"\nImage file not found"); 
    } 

    UIImage *image = [UIImage imageWithData:imageData]; 

    UIImageView *logo = [[UIImageView alloc] init]; 
    logo.image = image; 
    [self.view addSubview:logo]; 

} 

但這裏是日誌中的調試窗口

2017-07-14 18:26:35.679 IconShape[1089:348564] 
    dirPaths (
    "/Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents" 
    ) 

    docsDir 
    /Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents 
    2017-07-14 18:26:35.679 IconShape[1089:348564] 

    file path should be 

    /Users/gs/Library/Developer/CoreSimulator/Devices/57279C80-0937-4658-B0E6-7984B3768D56/data/Containers/Data/Application/18235DBF-7ADB-47D4-AFF9-282D02F2A0F8/Documents/iTunesArtwork1024.png 

    2017-07-14 18:26:35.679 IconShape[1089:348564] 
    Image file not found 

這裏是增加了兩個PNG文件後,該項目的狀態。 enter image description here 但是日誌顯示它們對NSFileManager不可見。那麼他們會在哪裏找到?即我需要對代碼進行哪些更改才能找到它們?


編輯

該草案認爲以下薩勃拉曼尼亞的建議PNG文件。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSFileManager *fileManager = [[NSFileManager alloc] init]; 

    NSString *imageFilePath  = [[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"]; 

    if ([fileManager fileExistsAtPath:imageFilePath]) 

    { 
     NSLog(@"\nFound file path : %@", imageFilePath); 
    } 

    else 

    { 
     NSLog(@"\nImage file not found"); 
    } 

    UIImage *image  = [UIImage imageNamed:@"iTunesArtwork1024.png"]; 
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)]; 
    logo.image   = image; 
    [self.view addSubview:logo]; 
} 

日誌現在讀取

 Found file path : … .app/iTunesArtwork1024.png 

和形象也出現在subview.

__

回答

2

圖片是不是在Document Directory,這是bundle內。

您已經在項目中添加了圖像文件。但是您正在檢查Document目錄上的圖像,這是錯誤的。圖片在app bundle之內。

您可以簡單地通過圖像的nameImage分配給UIImageView

UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"]; 

UIImageView *logo = [[UIImageView alloc] init]; 
logo.image = image; 

如果你想要得到的圖片的路徑,那麼你必須使用[NSBundle mainBundle]

[[NSBundle mainBundle]pathForResource:@"iTunesArtwork1024" ofType:@"png"]; 
+0

薩勃拉曼尼亞,進步。 iTunesArtwork1024.png出現在目錄中,但圖像仍然不顯示。查看我的編輯 – Greg

+0

Subramanian,你已經解釋並解決了我的問題。非常感激。這必須是被接受的答案。 – Greg

1

你可以用它們在項目中添加名稱訪問圖像。下面的代碼嘗試

UIImage *image = [UIImage imageNamed:@"iTunesArtwork1024.png"]; 
1

在您正在尋找的圖像的路徑是設備內部的(真實或模擬)。

要加載存儲在您的Xcode項目中的圖像只是做:

UIImage* image = [UIImage imageNamed:@"iTunesArtwork1024.png"];

相關問題