在iPad应用程序中存储多个图像文件

时间:2014-07-07 03:41:02

标签: xcode sqlite ipad uiimage

我应该将多个图像文件作为NSData存储到SQLite或
将物理文件保存到文件夹??

由于

1 个答案:

答案 0 :(得分:1)

将图像存储在应用程序目录中。这是处理的最佳和最简单的方法。请尝试以下代码。它会对你有帮助。

/Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

    return [data writeToFile:appFile atomically:YES];
}
//Image -  Retrieve from Application Directory

+(NSData *)readFromFile:(NSString *)fileName {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

NSData *myData = [NSData dataWithContentsOfFile:filePath]; 
if (myData) { 
    return myData;
}
return nil;
}