在iOS中存储下载图像的最佳方式 [英] Best way to store the downloaded images in iOS

查看:48
本文介绍了在iOS中存储下载图像的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种方式可以存储下载的图像?从那里我应该能够在我的应用程序的任何地方使用它们,并且在任何情况下都不应删除图像(如低空间)。请帮忙。

Which is the best way to store the downloaded images? From there I should be able to use them anywhere in my application, and images should not be deleted at any case (like low space). Any help please.

推荐答案

根据标准,App相关文件(Data)只需存储在文档目录中。图像获取后,将图像保存在文档目录中,并保留图像标识的唯一名称。

As per the standard, App related files(Data) need to be stored in document directory only. Once image get download store that images in document directory and maintain unique name for image identification.

-(NSString *)writeDataAsFile:(NSData *)imageData
{

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    NSString * thumbNailFilename = [NSString stringWithFormat:@"%@.png",[self GetUUID]]; // Create unique iD
    NSString * thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];

    if ([imageData writeToFile:thumbNailAppFile atomically:YES])
    {
        return thumbNailFilename;
    }

    return nil;
}

使用此方法将图像(下载的NSData)存储在文档目录中。

use this method to store the image(downloaded NSData) in document directory.

从文档目录中检索图像,如下所示

Retrieve image from the document directory like this

UIImage *thumbnailHomeImage = [UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"%@",imageName]];

这篇关于在iOS中存储下载图像的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆