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

- (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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值