use*_*474 2 objective-c nsfilemanager ios
我以这种方式将许多不同的图像保存到文档目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
int num = arc4random() % 100000000000000;
NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%dtest.png", num]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)
然后我用这种方式读取文件:
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSString* FinalPath = [documentsDirectory stringByAppendingPathComponent: @"Images/"];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:&error];
for(int i=0;i<[filePathsArray count];i++)
{
NSString *strFilePath = [filePathsArray objectAtIndex:i];
if ([[strFilePath pathExtension] isEqualToString:@"jpg"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"])
{
NSString *imagePath = [[stringPath stringByAppendingFormat:@"/"] stringByAppendingFormat:strFilePath];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
if(data)
{
@try {
NSString *nameOfGroup = [array objectAtIndex:i];
MWPhoto *photo;
photo= [MWPhoto photoWithFilePath:imagePath];
[finalPhotoArray addObject:photo];
}
@catch (NSException *exception) {
NSString *nameOfGroup = @"No description available for this sheet";
MWPhoto *photo;
photo= [MWPhoto photoWithFilePath:imagePath];
[finalPhotoArray addObject:photo];
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是图像没有按照我保存文件的顺序正确排序,但是它们混在一起.有没有办法订购它们?当我保存到文件目录时,文件是否混淆了?
对于contentsOfDirectoryAtPath:error:(这是非递归等效的subpathsOfDirectoryAtPath:error:)文档说:
返回数组中的文件顺序未定义.
如果要将图像路径与标题关联,则应将该关联存储在某处.如果您将字幕存储在NSStrings的NSArray中,则可以将它们存储在NSArray的NSDraries中.每个字典都有一个键@"imagePath"和@"caption".
此外,在处理表示路径的NSStrings时,您应该使用文档中使用路径下列出的方法.(例如stringByAppendingPathComponent:代替stringByAppendingFormat:)
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |