0

すべての画像を含むカスタム ディレクトリを作成します。構成のさまざまな場所で必要なときに画像を取得するのに役立つため、カスタム設計します。

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

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

/** 再度更新 **/

現在、このフォルダーは、Photoscroller の例に従って Application_Home/Resources/ImageTiles/ の下にあります。/Users/home/lifemoveson/test/ImageTiles/ フォルダーに変更するにはどうすればよいですか?

- (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;
}
4

2 に答える 2

0

iOS で実行されているアプリケーションはサンドボックス化されています。好きな場所にディレクトリを単純に作成することはできません。通話createDirectoryAtPath:は失敗します。代わりに、アプリケーション用に取っておいたディレクトリの 1 つを使用する必要があります。

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

于 2011-06-29T22:35:15.170 に答える
0

次のような関数を呼び出します。

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

次のように実装されています

-(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;
}

あなたの問題を解決するのに役立つかもしれません。ディレクトリの作成は、最初の関数を使用して実行されます。2 番目の関数を使用すると、アプリケーション バンドルから以前に作成したディレクトリのいずれかにファイルを保存できます。必要に応じて、アプリケーション バンドル以外の場所にある保存ファイルを変更できることを願っています。

于 2011-06-30T05:17:48.810 に答える