iOS将网络音频文件写入本地的方法

//创建保存文件路径 

fileName:文件名

type:文件类型

- (NSString*)getPathByFileName:(NSString *)fileName ofType:(NSString*)type{

//创建dcument文件路径

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

//完成创建文件路径

    NSString* fileDirectory = [[[directory stringByAppendingPathComponent:fileName]

                                stringByAppendingPathExtension:type]

                               stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

    return fileDirectory;

}

//文件的路径

NSString *self.amrPath;

       self.amrPath = [self getPathByFileName:model.CreateTime ofType:@"amr"];

//soundUrl为音频文件URL

       NSData *data = [NSDat dataWithContentsOfURL[[NSUR alloc]initWithString:SoundUrl]];

//写入文件中

      [data writeToFile:self.amrPath atomically:YES];





你可能感兴趣的:(iOS学习)