简体   繁体   English

存储大量图像和MP3歌曲iOS Sdk的最佳方法是什么

[英]what is the best way to store bulk of images and MP3 song iOS Sdk

i am doing one iphone app, for that i have to store bulk of MP3 song and images. 我正在做一个iPhone应用程序,为此我必须存储大量的MP3歌曲和图像。

can ay one tell me what is the best to store those in terms of performance. 可以告诉我,就性能而言,最好的存储方式是什么。

Store the image and songs in the application directory. 将图像和歌曲存储在应用程序目录中。 This is best and easy way to handle. 这是最好,最简单的处理方式。 Try the following code. 请尝试以下代码。 it will be help you. 它将为您提供帮助。

//Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // the path to write file
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

        return [data writeToFile:appFile atomically:YES];
}
//Image/songs -  Retrieve from Application Directory
+(NSData *)readFromFile:(NSString *)fileName {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

    NSData *myData = [NSData dataWithContentsOfFile:filePath]; 
    if (myData) { 
        return myData;
    }
    return nil;
}

将MP3歌曲和图像存储到您的应用程序项目的resources文件夹中,并在sqlite数据库中提供引用(因为在sqlite数据库中保存大文件不是一个好习惯)

我建议您将所有大尺寸项目存储在设备磁盘上,即Documents目录,并将其物理路径存储在核心数据或sqlite中,或者至少存储在plist文件中,以便您可以根据需要方便地检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM