以下代码可以正常工作,没有错误或异常 - 但它仍然没有做它应该做的!我想将图像保存到 iOS 库/应用程序支持文件夹中。更准确地说,应该将图像放置在 /library/Application Support/bundleID_name/subfolder/ 中(并且子文件夹称为“location1”)。
如果我使用 iOS 模拟器检查功能,我可以看到子文件夹的创建(即 .../library/Application Support/bundleID_name/location1/)。“saveImage”功能也毫无例外地工作。但是没有图像被保存!!!!(即图像文件丢失,文件夹为空)!!
可能是什么错误?
这是我调用两个函数的代码(见下面的代码):
UIImage *in_image = [UIImage imageNamed:@"template009c.jpg"];
NSString *locDirectoryName = @"location1";
NSURL *LocationDirectory = [self appendLocationToApplicationDirectory:locDirectoryName];
[self saveImage:in_image :@"image1" :LocationDirectory];
对应的函数-Nr1:
- (NSURL*)appendLocationToApplicationDirectory:(NSString*)locationDirName
{
NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;
// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0) {
// Append the bundle ID and the location-Foldername to the URL for the Application Support directory
dirPath = [[[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:appBundleID] URLByAppendingPathComponent:locationDirName];
// If the directory does not exist, this method creates it.
// This method call works in OS X 10.7 and later only.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError]) {
// Handle the error.
NSLog(@"%@", theError.localizedDescription);
return nil;
}
else {
// Mark the directory as excluded from iCloud backups
if (![dirPath setResourceValue:@YES
forKey:NSURLIsExcludedFromBackupKey
error:&theError]) {
NSLog(@"Error excluding %@ from iCloud backup %@", [dirPath lastPathComponent], theError.localizedDescription);
}
else {
NSLog(@"Location Directory excluded from iClud backups");
}
}
}
return dirPath;
}
和功能 Nr2:
//saving an image
- (void)saveImage:(UIImage*)image :(NSString*)imageName :(NSURL*)pathName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *LocationDirectory = [pathName absoluteString];
NSString *fullPath = [LocationDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
/***** THE FOLLOWING LINE DOES NOT SEEM TO DO WHAT IT IS SUPPOSED TO *******/
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
/**** I also tried without the FileManager, but same problem - no file written... ***/
// [imageData writeToFile:fullPath atomically:NO];
NSLog(@"image saved");
}
顺便说一句,使用 XCode-Debugger 获取“fullPath”,我得到:
"fullPath NSPathStore2 * @"file:/Users/username/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/2BCC3345-9M55F-4580-A1E7-6694E33456777/Library/Application%20Support/bundleID_name/image1.png" 0x09d50950
这似乎也不正确吗?但是为什么是 [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; 不表演???