1. 利用NSData的方法:
NSData *data = [NSData dataWithContentsOfURL:url];
这里要已知文件的URL,利用上面的方法把对应的URL的唯一资源写成二进制数据,在进行相应转换.
2.已知资源的URL,创建网络请求,在利用类NSURLConnection发送网络异步请求,
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
}];
3.也是已知资源的URL,创建网络请求,不过发送请求后是通过NSURLConnection的代理接受数据
#pragma mark- 代理方法
//1.接收到服务器的响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.fileData = [NSMutableData data];
//获得文件的总大小
self.totalSize = response.expectedContentLength;
}
//2.接收到服务器返回的数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.fileData appendData:data];
//计算文件的下载进度==当前已经下载的数据/文件的总大小
NSLog(@"%f",1.0 *self.fileData.length / self.totalSize);
self.progressView.progress = 1.0 *self.fileData.length / self.totalSize;
}