1

以下代码可以正常工作,没有错误或异常 - 但它仍然没有做它应该做的!我想将图像保存到 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]; 不表演???

4

2 回答 2

0

我很确定您无法将图像保存在您指定的路径上。您可以将图像保存在图库或 DocumentDirectory 中。这应该是在 DocumentDirectory 上保存图像的代码:

NSString *imgName=[@"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
于 2014-08-01T14:37:19.937 回答
0

这个:

"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

不是有效路径,它是 URL 路径,但存储在字符串中。如果您要使用 URL,请使用 ULR,而不是尝试转换为字符串:

[imageData writeToURL:pathName atomically:YES];

(最好将参数命名为pathURL),如果你想使用路径,那么在任何阶段都不要使用 URL。

此外,如果 API 方法返回错误或状态标志,请在代码中将其作为标准检查。

于 2014-08-01T14:43:47.477 回答