0

创建一个包含所有图像的自定义目录。设计它是定制的,因为它可以帮助我在配置中的不同位置获取图像。

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

我已将图像放在 test 文件夹下,并且属于 .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:通话将失败。您应该使用为您的应用程序预留的目录之一

一旦您获得了其中一个目录的路径,获取其中文件的路径只是使用NSString'sstringByAppendingPathComponent:方法的一种情况。

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

可能会帮助您解决问题。创建目录是使用第一个函数实现的,第二个函数可以让您将应用程序包中的文件保存到之前创建的任何目录中。我希望您可以修改它保存位于应用程序包以外的位置的文件以满足您的需要。

于 2011-06-30T05:17:48.810 回答