如何在iOS中更改URL中的内容? [英] How to vary the contents in URL in iOS?

查看:65
本文介绍了如何在iOS中更改URL中的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL,其中包含图像和音频文件.我正在使用以下代码将图像和音频文件下载到文档目录:

I am having a URL in which it contains images and audio files. I am downloading the images and audio files to document directory by using the below code:

  // urlsArray contains the list of images and audio urls

 for(int i= 0 ;i<[urlsArrray count]; i++)
        {

        NSString *urlString = [urlsArrray objectAtIndex:i];
        NSString *escapedUrlString = [urlString             stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:escapedUrlString];
    NSData *fileData=[NSData dataWithContentsOfURL:url];
    NSString *urlstring1 = [url absoluteString];
    NSArray *parts = [urlstring1 componentsSeparatedByString:@"/"];
    NSString *fileName = [parts objectAtIndex:[parts count]-1];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:fileName];
    [fileManager createFileAtPath:savedImagePath contents:fileData attributes:nil];

    NSLog(@"files Stored Path is %@",savedImagePath);

现在我要分离图像和音频文件.我想将图像下载到文档目录,将音频文件下载到缓存目录.

Now I want to seperate the images and audio files. I want to download images to document directory and audio files to cache directory.

推荐答案

只需添加以下代码即可识别文件是图像文件还是音频文件

Just add following code to recognize the file whether it is image or audio file

CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");
else  NSLog(@"It's audio");

这篇关于如何在iOS中更改URL中的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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