所以我有一个函数从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”时,它返回(null)。但是图像可以正确下载。
这是我的文档目录路径功能:
- (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。但是我做错了什么?谢谢。
答案 0 :(得分: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];
答案 1 :(得分:0)
这条线看起来不对我:
UIImage *testToSeeImageHasSaved = [UIImage imageNamed: [@"Documents/" stringByAppendingString: [properties objectForKey:@"home_venue_logo"]]];
我认为你必须这样做:
UIImage* image = [UIImage imageWithContentsOfFile:"YOUR_FILE_PATH"];
[UIImage imageNamed:"image_name"]
仅适用于附加到项目中的图片
答案 2 :(得分: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是用于保存它的路径,文件名是jpeg的名称。
答案 3 :(得分:0)
[UIImage imageNamed:@"Documents/filenamesavedas.jpeg"]
没有路径它会搜索你的包中有特定名称的文件。
使用对NSFileManager的调用找到该文件,并使用预期的路径和文件名。