2012-03-30 7 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); 

はしかし、コードのこの部分が設定されていませんイメージディレクトリに書き込もうとしている領収書イメージにイメージを表示します。

誰かが私が間違っていることとその修正方法を説明することはできますか?

おかげで、

ジャック

+1

あなたは 'の戻り値を確認しましたwriteToFile:アトミック: '?エラーがある場合は、 'writeToFile:options:error:'バリアントを使用してエラーを確認することができます。 – MrTJ

+0

また、実際にイメージをアプリケーションバンドルからドキュメントディレクトリにコピーする必要があるのはなぜですか? – MrTJ

+0

あなたはデバイス内のコードをチェックしていますか? シミュレータはコードを1行ずつ実行しないため、[imageData writeToFile:fullPathToFile atomically:NO];メソッドはパスに画像を書き込むために時間がかかるので、しばらくしてから画像に画像をロードしてみてください –

答えて

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]]; 
関連する問題