1

所以我有一个函数可以从如下所示的 URL 获取图像:

- (UIImage *) getImageFromURL:(NSString *)fileURL {
    UIImage * result;

    NSData * img_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:img_data];

    return result;
}

然后是一个从 UIImage 中保存图像的函数,如下所示:

    -(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
        NSLog(@"Image named %@ wrote to %@", imageName, directoryPath);
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

我面临的问题是,当我通过 saveImage 函数运行 UIImage 时,我认为它不会保存图像。这是因为当我检查 UIImage imageNamed:@"Documents/filenamesavedas.jpeg" 它返回(空)。但是,图像确实可以正确下载。

这是我的文档目录路径功能:

- (NSString *)getDocumentsDirectoryPath {
    NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(documentsDirectoryPath);
    return documentsDirectoryPath;
}

这是我正在下载并尝试保存图像的地方:

   dispatch_async(jsonQueue, ^{
        // check to see if data authenticate runs successfully. 
    NSString *documentsDirectoryPath = [util getDocumentsDirectoryPath];
    UIImage *imageToSave = [util getImageFromURL:[properties objectForKey:@"current_group_logoURL"]];
    [util saveImage:imageToSave withFileName:[properties objectForKey:@"home_venue_logo"] ofType:@"jpeg" inDirectory:documentsDirectoryPath];
    NSLog(@"Group Logo URL %@", [properties objectForKey:@"current_group_logoURL"]);
    NSLog(@"Current Group Image %@", [properties objectForKey:@"home_venue_logo"]);        
        UIImage *testToSeeItHasDownloaded = imageToSave;
        NSString *imageLocation = [documentsDirectoryPath stringByAppendingPathComponent:[properties objectForKey:@"home_venue_logo"]];
        NSData *data = [NSData dataWithContentsOfFile:imageLocation];
        UIImage *testToSeeImageHasSaved = [UIImage imageWithData:data];
        NSLog(@"testToSeeImagehasDownloaded: %@", testToSeeItHasDownloaded);
        NSLog(@"image location to save to: %@", imageLocation);
        NSLog(@"testToSeeImagehasSaved: %@", testToSeeImageHasSaved );    

    });

控制台返回这个:

2012-10-22 16:27:45.642 asdaf[46819:1d807] Group Logo URL http://somesite.com/50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.642 asdaf[46819:1d807] Current Group Image 50a0e42619424ba551a956511c098656.jpeg
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasDownloaded: <UIImage: 0xb09ed50>
2012-10-22 16:27:45.649 asdaf[46819:1d807] testToSeeImagehasSaved: (null)

testToSeeImagehasSaved 应该返回 null。但是我做错了什么?谢谢。

4

4 回答 4

1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//documentsDirectory is your documents directory path

//Now append your image name in this path
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"ImageName.jpeg"];

//convert your image in NSData
NSData *imageData = UIImagePNGRepresentation(image);
//Now write it at path
[imageData writeToFile:imagePath atomically:NO];

你可以用同样的方式得到这个图像

NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage imageWithData:data];

编辑:

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension 
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *imageName = [imageName stringByAppendingString:extension];
   //Note extension should be like .png, .jpg, .jpeg dont forget dot (.).

   NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

   NSData *imageData = UIImagePNGRepresentation(image);
   [imageData writeToFile:imagePath atomically:NO]; 
}

好吧,我现在不关心您的整个代码,而是为什么UIImage当您必须将图像保存在文档目录中时要传递对象。你也可以通过NSData你的方法。你做得更多。

您的问题是您每次都保存 .jpg 图像,并且在检索时您试图在同一路径上找到 .jpeg 图像,这就是您获得空值的原因。在您的保存方法中看到这一行 -

[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
于 2012-10-22T15:47:17.490 回答
0

这条线对我来说不合适:

UIImage *testToSeeImageHasSaved = [UIImage imageNamed: [@"Documents/" stringByAppendingString: [properties objectForKey:@"home_venue_logo"]]];

我认为你必须这样做:

UIImage* image = [UIImage imageWithContentsOfFile:"YOUR_FILE_PATH"];

[UIImage imageNamed:"image_name"]仅适用于附加到您的项目的图像

于 2012-10-22T15:45:26.220 回答
0

当您将其保存在文档文件夹中时,它不会将其完全保存在“文档”目录中,而是类似于

path=/var/mobile/Applications/144B3628-5258-4939-8B80-006EC5BE45B2/Library/Caches/6_1.png

因此,要检索您的图像,您应该使用

NSString *uniquePath = [directory stringByAppendingPathComponent: filename];
image = [UIImage imageWithContentsOfFile: uniquePath];

其中 directory 是您用来保存它的路径,而 filename 是您的 jpeg 的名称。

于 2012-10-22T15:48:04.170 回答
0
[UIImage imageNamed:@"Documents/filenamesavedas.jpeg"]

不采用路径,它会在您的捆绑包中搜索具有特定名称的文件。

使用调用 NSFileManager 找到文件并使用预期的路径和文件名。

于 2012-10-22T17:29:33.320 回答