_musicData = [NSMutableData data];
UIImageView *imageView = [UIImageView new];
imageView.frame = CGRectMake(0, 0, 200, 200);
imageView.center = self.view.center;
[self.view addSubview:imageView];
NSString *path = @"http://www.3337973.cc/imgall/mjuxu2djfy2diojtfzrw63i/uploads/allimg/140828/3-140RQ34K1.jpg";
NSLog(@"-----out");
__block NSData *imageData;
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
UIImage *image = [UIImage imageWithData:data];
imageData = data;
imageView.image= [UIImage imageWithData:data];
NSLog(@"err:%@",connectionError);
}];
NSLog(@"---->%@",imageData);
@interface ViewController ()<NSURLConnectionDataDelegate>
{
NSMutableData *_musicData;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path2 = @"http://link.hhtjim.com/163/145662.mp3";
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path2]] delegate:self startImmediately:NO];
[connection start];
}
//找到歌曲的总大小
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"response:%@",response);
NSLog(@"--fileSize:%lld",response.expectedContentLength);
}
//每次下载数据的大小
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"data-->%ld",data.length);
[_musicData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"done finish");
[_musicData writeToFile:@"/Users/frankfan/desktop/留什么给你.mp3" atomically:YES];
}
@end
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。