gpt4 book ai didi

iphone - 试图将 jpeg 保存到应用程序的文档目录

转载 作者:行者123 更新时间:2023-11-29 13:26:25 25 4
gpt4 key购买 nike

所以我有一个函数可以从 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。但是我做错了什么?谢谢。

最佳答案

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];

关于iphone - 试图将 jpeg 保存到应用程序的文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13014845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com