2012-03-30 42 views
1

我使用這段代碼,試圖將圖像保存到文件目錄:的Objective-C:無法寫入圖像文檔目錄

//FOR TESTING ON SIMULATOR 
UIImage* image = [UIImage imageNamed:@"ReceiptImage1.jpg"]; 
NSData *imageData = UIImagePNGRepresentation(image); 
NSString* imageName = @"Receipt1Image1.png"; 
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0]; 
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];   
[imageData writeToFile:fullPathToFile atomically:NO]; 

self.receiptImage1 = fullPathToFile; 

self.receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPathToFile]; 

NSLog(@"fullPathToFile %@", fullPathToFile); 

然而這段代碼不設置imageViews圖像到我試圖寫入文檔目錄的收據圖像。

有人可以解釋我做錯了什麼,以及如何解決它?

感謝,

傑克

+1

將writeToFile:原子:'?如果出現錯誤,可以使用'writeToFile:options:error:'variant來檢查它是什麼。 – MrTJ 2012-03-30 10:38:23

+0

另外,爲什麼你實際上需要將應用程序包中的圖像複製到文檔目錄中? – MrTJ 2012-03-30 10:39:33

+0

你有沒有檢查你的設備代碼? 因爲模擬器不會逐行執行代碼.. [imageData writeToFile:fullPathToFile atomically:NO];方法花費一些時間將圖像寫入路徑,因此嘗試在一段時間後在圖像中加載圖像 – 2012-03-30 10:39:52

回答

6

使用此:

- (void)saveImage:(UIImage*)image:(NSString*)imageName{ 
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 

NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 

NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) 

NSLog(@"image saved");} 
0

首先將文件寫入原子,然後嘗試:

self.receiptImage1 = [UIImage imageWithContentsOfFile:fullPathToFile]; 
self.receiptImageView1 = [UIImageView initWithImage:self.receiptImage1]; 
0

它可能不會保存到該路徑,如果有已經是文件了。 (檢查API文檔,我記不太清楚了,但要保存你應該增加類似:

NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [dirs objectAtIndex:0]; 
    _exportPath = [documentsDirectoryPath stringByAppendingPathComponent:aFileName]; // may need to hang onto him 

    if ([[NSFileManager defaultManager] fileExistsAtPath:_exportPath]) { 
     [[NSFileManager defaultManager] removeItemAtPath:_exportPath 
                error:nil]; 
    } 
0

你可以採取以下方法

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath { 
    if ([[extension lowercaseString] isEqualToString:@"png"]) { 
     [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil]; 
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) { 
     [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil]; 
    } else { 
     ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension); 
    } 
} 

要保存圖像,簡單地做它是這樣的:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path]; 

要加載圖像,實現此方法:

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath { 

    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]]; 

    return result; 
} 

,然後用它是這樣的:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
yourImage.image = [self loadImage:@"Your Image Name" ofType:@"png" inDirectory:path]; 

或者你可以簡單地設置你的形象等於一下這個方法返回而不是創建一個方法:你檢查的`返回值

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
yourImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Your Image Name.png", path]];