次のコードは、エラーや例外なく機能しますが、それでも、本来の機能を実行していません! 画像を iOS ライブラリ/Application Support フォルダに保存したかったのです。より正確には、イメージは /library/Application Support/bundleID_name/subfolder/ (およびサブフォルダーは「location1」と呼ばれます) に配置する必要があります。
iOS-Simulator で機能を確認すると、サブフォルダー (つまり .../library/Application Support/bundleID_name/location1/) が作成されていることがわかります。また、関数「saveImage」も例外なく機能します。しかし、保存されている画像はありません!!!! (つまり、画像ファイルが見つからず、フォルダーは空のままです) !!
何が間違いでしょうか??
2 つの関数を呼び出すコードを次に示します (以下のコードを参照)。
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]; 実行しない???