如何下载一些图片放到temp目录下的Imgs文件夹下

时间:2021-03-22 21:38:40
- (void)viewDidLoad {
    [super viewDidLoad];
    //tmp:存放临时数据,此目录下的数据不会通过iCloud同步
    //下载一些图片放到tmp目录下的Imgs文件夹下,如果有些图片已经下载,那么不会继续下载
//  
//    NSString *tmpString = NSTemporaryDirectory();
//    NSLog(@"tmpString = %@",tmpString);

        NSArray *imgsArray = @[@"http://d.hiphotos.baidu.com/image/pic/item/6a63f6246b600c331c964cf21d4c510fd9f9a119.jpg",@"http://c.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce9c017233b1003af33a87b219.jpg",@"http://f.hiphotos.baidu.com/image/pic/item/e1fe9925bc315c60d916f9d58ab1cb134954770d.jpg",@"http://h.hiphotos.baidu.com/image/pic/item/30adcbef76094b366b2389d7a4cc7cd98d109d53.jpg",@"http://d.hiphotos.baidu.com/image/pic/item/b17eca8065380cd72aaf30d7a644ad3459828153.jpg"];
    
    NSString *imgsTmpPath = [self creatDirInTmp:@"Imgs"];
    if (imgsTmpPath != nil)
    {
        
        for (int i = 0; i < imgsArray.count; i++)
        {
            NSString *imgsString = [imgsArray[i]lastPathComponent];
            //获取每一张图片的路径
            NSString *imgsPath = [imgsTmpPath stringByAppendingPathComponent:imgsString];
    
            //用单例类  NSFileManager的对象,将文件写入本地
          NSFileManager *fileManager = [NSFileManager defaultManager];
            
            if (![fileManager fileExistsAtPath:imgsPath])
            {
            
             //进行编码
                NSString *urlString = [imgsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//            NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
//           NSString *urlString = [imgsArray[i] stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
            //我们的图片 视频 音频等在网络中都是以二进制文件传输,所以我们这里拿到的是data
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
            if (data == nil)
            {
                NSLog(@"网络有问题,请稍后重试");
            }
            else
            {
              BOOL isSuccess = [data writeToFile:imgsPath atomically:YES];
                if (isSuccess)
                {
                    NSLog(@"图片下载成功");
                }
                else
                {
                    NSLog(@"图片下载失败");
                }
            }
        }
    }
    
}

}



//这里我们封装一个函数,使得这个函数返回的是我们在tmp目录下想要的文件夹的路径
-(NSString *)creatDirInTmp:(NSString *)dirName
{
    //获取根目录文件路径
    NSString *tmp = NSTemporaryDirectory();
    
    //拼接成我们想要的文件的路径的字符串
    NSString *dirPath = [tmp stringByAppendingPathComponent:dirName];
    //获取NSFileManager 单例类,用文件操作
    NSFileManager *fileManage = [NSFileManager defaultManager];
    //判断是否存在某个文件或文件夹
    if (![fileManage fileExistsAtPath:dirName])
    {
        //创建文件夹
        NSError *error;
      BOOL isSuccess = [fileManage createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (!isSuccess)
        {
            dirPath = nil;
            NSLog(@"error = %@",error.debugDescription);
        }
    }
    return dirPath;
}