2011-06-29 20 views
0

すべての画像を含むカスタムディレクトリを作成します。なぜなら、設定のさまざまな場所で必要なときにイメージを取得するのに役立つからです。NSBundleを使用してカスタムディレクトリから画像ファイルを読み込みます。

NSFileManager *filemgr; 
filemgr = [NSFileManager defaultManager];  
[filemgr createDirectoryAtPath: @"/Users/home/lifemoveson/test" withIntermediateDirectories:YES attributes: nil error:NULL]; 

私は画像をテストフォルダの下に置き、.pngタイプです。 以下のような画像を取得する方法はありますか?

/**/

現在、このフォルダはPhotoscrollerの例のようにAPPLICATION_HOME /リソース/ ImageTiles /下で再び更新。 /Users/home/lifemoveson/test/ImageTiles/folderに変更するにはどうしたらいいですか?

- (UIImage *)tileForScale:(CGFloat)scale row:(int)row col:(int)col 
{ 
// we use "imageWithContentsOfFile:" instead of "imageNamed:" here because we don't want UIImage to cache our tiles 
NSString *tileName = [NSString stringWithFormat:@"%@_%d_%d_%d", imageName, (int)(scale * 1000), col, row]; 

// Currently this folder is under <Application_Home>/Resources/ImageTiles/ as per Photoscroller example. 
// How can we change it to /Users/home/lifemoveson/test/ImageTiles/ folder ? 
NSString *path = [[NSBundle mainBundle] pathForResource:tileName ofType:@"png"]; 
UIImage *image = [UIImage imageWithContentsOfFile:path]; 
return image; 
} 
+0

解決方法はありますか?同じ問題に直面している – priya

答えて

0

iOSで実行されているアプリケーションはサンドボックス化されています。あなたは単にあなたが好きな場所にディレクトリを作成することはできません。 createDirectoryAtPath:コールは失敗します。代わりにone of the directories set aside for your applicationを使用してください。

これらのディレクトリのいずれかのパスを取得すると、その中のファイルのパスを取得することは、単純にNSStringstringByAppendingPathComponent:メソッドを使用する場合です。

+0

こんにちはジム。私は本当に最初にiosが私が好きなディレクトリからファイルを読むことができないと考えました。私は私の質問を更新しました。 NSStringのstringByAppendingPathComponentを使って画像を取得するにはどうすればいいですか? – lifemoveson

+0

あなたはどこにいるのですか? – Jim

+0

/lifemoveson/testまたは/lifemoveson/test1または/lifemoveson/test2のような名前で、さまざまなフォルダに画像を挿入しようとしています。将来、どのフォルダを調べるかはパス名を気にすることなく知ることができます。 – lifemoveson

0

マキン

-(NSString*)createDirectoryWithName:(NSString*)dirName{ 

    NSArray* directoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES); 
    NSString* directoryPath = [directoryArray objectAtIndex:0]; 
    NSString* newPath = [directoryPath stringByAppendingString:[NSString stringWithFormat:@"/%@",dirName]]; 
    NSFileManager *filemamager = [NSFileManager defaultManager];  
    BOOL flag = [filemamager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes: nil error:NULL]; 
    return flag == YES ?newPath: nil; 
} 

-(BOOL)saveFile:(NSString*)fileName atPath:(NSString*)path{ 

    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the File and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the File has already been created in the users filesystem 
    NSString *filePath = [path stringByAppendingPathComponent:fileName]; 

    success = [fileManager fileExistsAtPath:filePath]; 

    // If the File already exists then return without doing anything 
    if(success) return YES; 

    // If not then proceed to copy the File from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *pathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; 

    // Copy the database from the package to the users filesystem 

    NSError *error = nil; 

    BOOL flag = [fileManager copyItemAtPath:pathFromApp toPath:filePath error:&error]; 

    return flag; 
} 

はあなたの問題を解決するのに役立つかもしれない次のように実装されているように

NSString* newDirPath = [self createDirectoryWithName:@"Test"]; 
if (newDirPath) { 
    [self saveFile:@"MasterDB.sqlite" atPath:newDirPath]; 
} 

などの機能への呼び出し。ディレクトリを作成するには最初の関数を使用し、2番目の方法ではアプリケーションバンドルのファイルを以前に作成したディレクトリに保存できます。アプリケーションバンドル以外の場所にあるファイルを必要に応じて保存するように変更することをお勧めします。

+0

Rahulさんありがとうございます。しかし、私はまだ正確ではないと思う。私は再び質問を変えました。上記のシナリオが可能かどうか教えてください。 – lifemoveson

+0

ディレクトリをどこに作成したいのか分かりません。デバイス上で動作するアプリケーションのドキュメントフォルダにカスタムディレクトリを作成できます。 @ "/ Users/home/lifemoveson/test"というパスはあいまいです。 –

+0

iphoneデバイスはサンドボックスのようなものなので、アプリケーションフォルダ内にのみドキュメントフォルダを作成できます。したがって、私の質問はあいまいです。 – lifemoveson

関連する問題