0
嗨我想保存图像到一个目录,我通过NSData并做我认为将文件保存在我创建的目录中,但问题是,它不保存。这是我迄今为止所拥有的。为什么没有initWithContentsOfURL:encoding:error:work,它会返回null,但我使用的另一种方法是有效的?主要问题是WRITETOURL返回0,我认为这意味着信息没有正确存储,任何提示?保存图像到一个目录
NSFileManager *fm = [[NSFileManager alloc] init];
NSArray * directoryPaths = [fm URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
NSLog(@"%@", directoryPaths);
NSURL* dirPath = nil;
dirPath = [[directoryPaths objectAtIndex:0] URLByAppendingPathComponent:[NSString stringWithFormat:@"photos.jpeg"]];
NSError* theError = nil;
[fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError];
UIImage* photoToStore = [UIImage imageWithData:photoToSave];
NSString *pathContainingPhoto = [[NSString alloc] initWithFormat:@"%@.jpeg", UIImageJPEGRepresentation(photoToStore, 1.0)];
NSError *error = nil;
BOOL OK = [pathContainingPhoto writeToURL:dirPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"OK = %d", OK); //Returns 0
NSLog(@"%@", dirPath);
//WHY DOESNT THIS VERSION WORK?
// NSString *pathToFile = [[NSString alloc] initWithContentsOfURL:dirPath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", pathToFile);
NSString* pathToFile = [NSString stringWithContentsOfURL:dirPath encoding:nil error:nil];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:pathToFile error:nil];
NSLog(@"%@", dirContents);
怎么写writetoURL不起作用,但是writeetoFile呢? – 2012-08-17 17:12:39
也它声称NSData不宣布WRiteToFile? – 2012-08-17 17:23:15
如果我将多个图像保存在一个文件中,又如何获得特定数据的内容? – 2012-08-17 17:29:53