保存和加载图片的正确方法

发布于 2024-10-19 18:00:28 字数 226 浏览 2 评论 0原文

我正在制作一个小应用程序,用户可以在其中创建游戏配置文件、输入一些数据和可以用相机拍摄的图片。

我在 NSUserDefaults 的帮助下保存了大部分个人资料数据,但一位朋友阻止我将个人资料图像保存在 NSUserDefault 中。

在我的应用中本地保存和检索图像的正确方法是什么?

I am making a small app where the user can create a game profile, input some data and a picture that can be taken with the camera.

I save most of that profile data with the help of NSUserDefaults, but a friend discouraged me from saving the profile image in NSUserDefault.

What's the proper way to save and retrieve images locally in my app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你列表最软的妹 2024-10-26 18:00:28

您应该将其保存在DocumentsCache文件夹中。以下是具体操作方法。

保存Documents文件夹:

NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/myImage.png"];

BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path 
          contents:nil attributes:nil];

if (!ok) 
{
    NSLog(@"Error creating file %@", path);
} 
else 
{
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
   [myFileHandle writeData:UIImagePNGRepresentation(yourImage)];
   [myFileHandle closeFile];
}

Documents文件夹加载

NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];

您还可以使用UIImageJPEGRepresentation< /code>UIImage 保存为 JPEG 文件。此外,如果您想将其保存在 Cache 目录中,请使用:

[NSHomeDirectory() stringByAppendingString:@"/Library/Caches/"]

You should save it in Documents or Cache folder. Here is how to do it.

Saving into Documents folder:

NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/myImage.png"];

BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path 
          contents:nil attributes:nil];

if (!ok) 
{
    NSLog(@"Error creating file %@", path);
} 
else 
{
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
   [myFileHandle writeData:UIImagePNGRepresentation(yourImage)];
   [myFileHandle closeFile];
}

Loading from Documents folder:

NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];

You can also use UIImageJPEGRepresentation to save your UIImage as a JPEG file. What's more if you want to save it in Cache directory, use:

[NSHomeDirectory() stringByAppendingString:@"/Library/Caches/"]
抽个烟儿 2024-10-26 18:00:28

一种方法是使用应用程序的文档目录。这是特定于应用程序的,对其他应用程序不可见。
如何创建这个:
只需向 App Delegate 添加一个静态函数,然后在需要路径的地方使用该函数即可。

- (NSString )applicationDocumentDirectory {
    /
     Returns the path to the application's documents directory.
     */ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

Hope it Helped..

One way to do this is use the application's document directory. This is specific to a application and will not be visible to other applications.
How to create this:
Just add a static function to App Delegate and use the function where ever the path is required.

- (NSString )applicationDocumentDirectory {
    /
     Returns the path to the application's documents directory.
     */ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}


Hope it Helped..

祁梦 2024-10-26 18:00:28

我认为这个("iphone-user-defaults-and-uiimages")发布地址你的问题。不要将 blob 保存到属性列表,例如 NSUserDefaults。在你的情况下,我会直接写入磁盘。

I think this("iphone-user-defaults-and-uiimages") post addresses your issue. Don't save blobs to a property list such as NSUserDefaults. In your case I would write to disk directly instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文