2012-10-11 9 views
5

デバッグに役立つ簡単なプログラムを書きました。[NSData writeToFile]はどこに書き込みますか?

#import "UIImage+saveScreenShotOnDisk.h" 

@implementation UIImage (saveScreenShotOnDisk) 

-(void)saveScreenshot{ 
    NSData * data = UIImagePNGRepresentation(self); 
    [data writeToFile:@"foo.png" atomically:YES]; 
} 

@end 

それが実行されます後、私はfoo.pngがどこにあるかを知りたいです。

私は

~Library/Application Support 

に行って、私はfoo.pngを見つけることができません。それはどこにある?

私は

BOOL result = [data writeToFile:@"foo.png" atomically:YES]; 

をすれば結果は一種の奇妙な与えられたシミュレータは、iPhoneと違って、どこにでも書くことができるということですNO、となります。

+1

それは、一般的には、あなたの現在の作業ディレクトリ(CWD)、ですあなたのプログラムが立ち上げられた場所。 – sidyll

+0

それはありません、私はBOOL結果= [データwriteToFile:@ "foo.png" atomically:はい]を行う場合。結果はNOです。 –

+0

ここを参照してください:[http://stackoverflow.com/questions/5794422/iphone-save-uiimage-to-desktop-on-simulator] [1] [1]:のhttp://のstackoverflow。 com/questions/5794422/iphone-save-uiimage-to-desktop-on-simulator – smoothdvd

答えて

19

あなたはデータが保存されるようにしたいパスにNSDataオブジェクトを指示する必要があります。

NSString *docsDir; 
NSArray *dirPaths; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"foo.png"]]; 
[data writeToFile:databasePath atomically:YES]; 

これは、デバイス(またはシミュレータ)の上に、あなたのアプリケーションのフォルダにファイルを保存します。

+1

これは私が最終的にしたものです。パスを指定しないと、「デフォルト」のパスになると思っていました。私は間違っていた。 –

+1

すごくうれしいです。 – Shachar

1

U UがこのWRITETOFILEをwrite.Checkする場所または経路を提供するのを忘れ:オプション:エラー:

は、指定されたパスで指定されたファイルに受信機のバイトを書き込みます。

- (BOOL)writeToFile:(NSString *)path options:(NSDataWritingOptions)mask error:(NSError **)errorPtr 

パラメータ

パス

The location to which to write the receiver's bytes. 

マスク

A mask that specifies options for writing the data. Constant components are described in “NSDataWritingOptions”. 

errorPtr

If there is an error writing out the data, upon return contains an NSError object that describes the problem. 
1

あなたが「NSDocumentsDirectory」の検索を与えることができ、私は通常、このメソッドを使用してドキュメントディレクトリのパスを検索し、そこに私のファイル名を追加します。メソッドwriteToFile:では、この追加パスが提供されます。次に、iTunes>アプリケーションを使用して、下にスクロールし、あなたのアプリを選択すると、保存されたファイルを見ることができるはずです。

PS:電話機ストレージを使用するアプリケーションを指定するplistの値を設定する必要があります。

5

ファイルが格納されるデフォルトの場所は、実行可能ファイルが格納されているフォルダです。その後

Screenshot - show in finder executable

Finderで開いて:あなたの実行可能ファイルは製品 - > YourExecutableを右クリックを経由して格納されている場所

を参照してください。ここで

pawel.plistリソースはShacharの回答に基づいて、スウィフトのために

NSArray *a = @[@"mybike", @"klapki", @"clapki", @"ah"]; 
[a writeToFile:@"pawel.plist" atomically:NO]; 

pawel.plist resource I done from code

2

を経由して作成します。

let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] + "/foo.png" 
data?.writeToFile(path, atomically: true) 
関連する問題