图片下载的两种方式

来源:互联网 发布:云杉网络融资 编辑:程序博客网 时间:2024/05/23 00:06

图片下载的两种方式

                                                                                                                        //三方库下载

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSURL *URL = [NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    //NSLog(@"图片地址:%@",request);
    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible =YES;//转动
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
                                              {
                                                  NSString *filenamepath=[NSHomeDirectory() stringByAppendingPathComponent:@"img/"];
                                                  NSFileManager *fileManager = [NSFileManager defaultManager];
                                                  if([fileManager fileExistsAtPath:filenamepath])//检查文件夹是否存在
                                                  {//文件夹存在
                                                      NSLog(@"文件夹存在");
                                                  }
                                                  else
                                                  {//文件夹不存在
                                                      NSLog(@"文件夹不存在");
                                                      //创建路径
                                                      [fileManager createDirectoryAtPath:filenamepath withIntermediateDirectories:YES attributes:nil error:nil];
                                                  }
                                                  NSURL *documentsDirectoryPath=[NSURL fileURLWithPath:filenamepath];
                                                  //返回你要保存的路径
                                                  return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
                                              }completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
                                              {
                                                  if(filePath==nil)
                                                  {
                                                      //图片地址空
                                                      NSLog(@"图片地址空,request:%@",request);
                                                      NSLog(@"error:%@",error);
                                                  }
                                                  else
                                                  {
                                                      NSLog(@"File downloaded to: %@", filePath);
                                                  }
                                              }];

    [downloadTask resume];


                                                                                                //普通下载

//创建保存图片文件夹
            NSString *document = [NSHomeDirectory() stringByAppendingString:@"/Documents"];//找到沙盒中的Documents文件夹
            NSFileManager *manager = [NSFileManager defaultManager];
            BOOL isDirectory = [manager createDirectoryAtPath:[document stringByAppendingString:@"/pictureDownland"] withIntermediateDirectories:YES attributes:nil error:nil];//第一个参数是这次要创建的文件夹的完整路径
            if(isDirectory)
            {
                NSLog(@"目录创建成功");
            }
            else
            {
                NSLog(@"目录创建失败");
            }
            //异步下载图片
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            Model *model = self.dataArr[current-1];
            [img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecy.cms.palmtrends.com%@",model.icon]]];
            //获取当前时间
            NSData *imagedata=UIImagePNGRepresentation(img.image);
            NSDate *date = [NSDate date];
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//一个日历对象
            NSDateComponents *com = [[NSDateComponents alloc] init];//一个日历中元素的对象
            //设置日历元素对象的集合,(选择你想要用的那些元素)
            NSInteger flag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit;
            com = [calendar components:flag fromDate:date];//第一个参数就是上面做的元素对象的集合
            int year = [com year];
            int month = [com month];
            int day = [com day];
            int hour = [com hour];
            int minute = [com minute];
            int second = [com second];
            //存储图片
            [manager createFileAtPath:[[NSHomeDirectory() stringByAppendingString:@"/Documents/pictureDownland"] stringByAppendingString:[NSString stringWithFormat:@"/image_%d%d%d%d%d%d.png",year,month,day,hour,minute,second]] contents:imagedata attributes:nil];    //将图片保存为PNG格式
            //提示
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            break;


//单纯的提示框
#define AlertViewShow(content) UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"提示" message:content delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];\
[alerVier show];\
[alerVier release]

这个是alert的宏定义


0 0
原创粉丝点击