iOS 通过网络请求获取图片的下载歌曲
1.导入代理<NSURLConnectionDataDelegate>
1 @interface ViewController ()<NSURLConnectionDataDelegate> 2 { 3 long long alllength; //下载总长度 4 long long currlenth; //当期下载长度 5 } 6 //存放下载的mp3 数据流 7 @property(nonatomic,strong)NSMutableData *msicDate; 8 //下载进度显示 9 @property(nonatomic,strong)UIProgressView *msicProgress; 10 //下载按钮 11 @property(nonatomic,strong)UIButton *but; 12 //音乐播放 13 @property(nonatomic,strong)AVAudioPlayer *player;
1 //添加下载图片地址 2 NSString *path=@"http://pic1.nipic.com/2008-10-27/200810278105878_2.jpg"; 3 4 //调用下载图片方法 5 [self addTheimg:path]; 6 7 //进度条设置 8 self.msicProgress =[[UIProgressView alloc]initWithFrame:CGRectMake(60, 40, 230, 10)]; 9 self.msicProgress.progress=0; 10 //设置下载进度颜色 11 self.msicProgress.progressTintColor=[UIColor colorWithRed:0.007 green:0.868 blue:0.000 alpha:1.000]; 12 //进度条背景颜色 13 self.msicProgress.trackTintColor=[UIColor grayColor]; 14 15 //下载按钮点击下载 16 self.but=[UIButton buttonWithType:UIButtonTypeCustom]; 17 _but.frame=CGRectMake(140, 55, 40, 20); 18 [_but setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal]; 19 [_but setTitle:@"下载" forState:UIControlStateNormal]; 20 _but.backgroundColor=[UIColor orangeColor]; 21 [_but addTarget:self action:@selector(plarmusic:) forControlEvents:UIControlEventTouchUpInside]; 22 23 [self.view addSubview:_msicProgress]; 24 [self.view addSubview:_but];
方法的实现
1 //按钮点击时间相应方法 2 -(void)plarmusic:(UIButton *)sender 3 { 4 //歌曲下载总的长度 5 alllength=0; 6 //当前获取的量 7 currlenth = 0; 8 self.msicDate =[[NSMutableData alloc]init]; 9 //设置歌曲下载的地址 10 NSString *path=@"http://yinyueshiting.baidu.com/data2/music/124345627/124104509190800128.mp3?xcode=cb12e87f8333e5370d9b8f0c677c76d2"; 11 NSURL *url=[NSURL URLWithString:path]; 12 //方式1 网络请求 13 NSURLRequest *requst=[NSURLRequest requestWithURL:url]; 14 //方式2 15 //NSURLRequest *resq=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 16 17 //使用代理进行请求 创建连接服务器 18 [NSURLConnection connectionWithRequest:requst delegate:self]; 19 }
1 //实现网络请求代理方法 2 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 3 { 4 //获取要下载的MP3 的长度,收到响应即可得知,不用下载完毕 5 //此代理方法只执行一次,获取总的数据量 6 alllength =[response expectedContentLength]; 7 } 8 9 //获取每次下载的数据量,此方法可执行多次 10 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 11 { 12 //将每次下载的数据放入总得数据区域内 13 [self.msicDate appendData:data]; 14 //将本次获取的数据长度追加到当期长度上 15 currlenth +=data.length; 16 //更新进度条 17 self.msicProgress.progress=(float)currlenth/alllength; 18 } 19 20 //下载结束调用,播放音乐 21 -(void)connectionDidFinishLoading:(NSURLConnection *)connection 22 { 23 NSError *error=nil; 24 //下载完成后,初始化player 然后进行歌曲播放 25 self.player = [[AVAudioPlayer alloc]initWithData:self.msicDate error:&error]; 26 [self.player play]; 27 } 28 29 //请求错误调用 30 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 31 { 32 if (error) { 33 NSLog(@"error == %@",error); 34 } 35 }
1 //调用图片的方法 2 -(void)addTheimg:(NSString *)path 3 { 4 // 1.通过字符串创建一个url 5 NSString *imgstr=path; 6 NSURL *imgurl=[NSURL URLWithString:imgstr]; 7 8 //2. 创建网络请求 9 NSURLRequest *requst=[NSURLRequest requestWithURL:imgurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 10 11 NSError *error=nil; 12 //3. 链接服务器 13 NSData *imgDate = [NSURLConnection sendSynchronousRequest:requst returningResponse:nil error:&error]; 14 if (error) 15 { 16 //如果错误,打印错误 17 NSLog(@"%@",error); 18 }else 19 { 20 //如果找到图片,创建一个图片视图接收图片 21 UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(5, 20, 50, 50)]; 22 img.image=[UIImage imageWithData:imgDate]; 23 //将图片显示在视图 24 [self.view addSubview:img]; 25 } 26 27 }