OC学习:NSURL讲解和OC中图片下载

/*
         1.HTTP 协议
         2.file协议,本地文本协议
         3.FTP协议,文件传输协议
         4.SSH协议,加密和远程登陆
         5.SMP协议,邮件通信
         6.talent协议,交互式远程登陆回话
         
         
         URL用于标识Internet上的资源位置。
         语法:
         protocol://username@hostname:por/path/filename?quera#fragment
         1.protocol协议可以是上面的协议
         2.username代表服务器用户名,可选参数
         3.hostname代表服务器名称,可以是名称,也可以是‘节点地址’
         4.prot端口,可选参数,如果是http协议,默认是80,FTP协议默认是21
         5.path/filename服务器中文件的路径
         6.query#fragment查询条件,通常用于向服务器提供附加参数。一般只用于http URL,这里的查询条件是向服务器程序提供参数
         7.absoluteString 绝对路径
         
         URL
         1.不能出现中文等一些特殊字符。
         
         */
        NSURL *url1 = [NSURL URLWithString:@"http://iosseventeenclass.sinaapp.com/index.php?name=vincent&age=25"];
        
        //  如果网址出现中文,空格,%等
        NSString  *urlstring = @"http://iosseventeenclass.sinaapp.com/index.php?name=大神&age=25";
        //进行编码,一般情况下都要编码一下较好即使没有出现中文和一些特殊字符,stringByAddingPercentEscapesUsingEncoding:等参数是一个枚举。
        urlstring = [urlstring stringByAddingPercentEscapesUsingEncoding:4];
        //初始化
        NSURL * url2 = [NSURL URLWithString:urlstring];
        
        NSLog(@"1%@",url2.absoluteString);
        NSLog(@"2%@",url2.relativeString);
        NSLog(@"3%@",url2.scheme);
        NSLog(@"4%@",url2.resourceSpecifier);
        NSLog(@"5%@",url2.host);
        NSLog(@"6%@",url2.port);
        NSLog(@"7%@",url2.user);
        NSLog(@"8%@",url2.password);
        NSLog(@"9%@",url2.query);
        NSLog(@"0%@",url2.fragment);
        
        //在此我想要下载一张图片
        //1拿到一个关于图片的网上地址
        NSString *mystring = [NSString stringWithFormat:@"http://img.firefoxchina.cn/2015/08/5/201508031026420.jpg"];
        
        //2编码
        mystring = [mystring stringByAddingPercentEscapesUsingEncoding:4];
        //3初始化出一个URL
        NSURL *myurl = [NSURL URLWithString:mystring];
        //4拿到图片的二进制编码
        NSData *data = [NSData dataWithContentsOfURL:myurl];
        //5将图片下载到路径/Users/yf01-d15/Desktop/(打开终端,将一个本地桌面的任意一个文本文件拉到终端,在终端中就可以看到本地桌面的路径),下载后的文件名是wo.jpg,其中writeToFile方法有返回值bool类型,判断方法是否成功执行,试一试,你会下载到一张你想要的图片。
        BOOL fag = [data writeToFile:@"/Users/yf01-d15/Desktop/wo.jpg" atomically:YES];
在 iOS 开发,可以使用 AVPlayer 来播放音频,但是默认情况下 AVPlayer 不支持音频下载功能。不过,我们可以通过一些方法来实现音频下载。 一种方法是使用 AVAssetExportSession,将音频文件导出为本地文件。具体步骤如下: 1. 创建 AVURLAsset 对象,用于获取音频资源。 2. 创建 AVAssetExportSession 对象,用于导出音频资源。 3. 设置 AVAssetExportSession 的输出路径和输出格式。 4. 调用 exportAsynchronouslyWithCompletionHandler 方法开始导出音频资源。 5. 在 completion handler 获取导出的本地音频文件路径,然后使用 AVPlayer 播放该文件。 示例代码如下: ```objc // 获取音频资源 AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:@"http://example.com/audio.mp3"]]; // 创建导出会话 AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetAppleM4A]; // 设置输出路径和格式 NSString *outputPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"audio.m4a"]; exportSession.outputURL = [NSURL fileURLWithPath:outputPath]; exportSession.outputFileType = AVFileTypeAppleM4A; // 开始导出 [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch (exportSession.status) { case AVAssetExportSessionStatusCompleted: { // 获取导出的本地文件路径 NSString *localPath = exportSession.outputURL.path; // 使用 AVPlayer 播放本地文件 AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:localPath]]; AVPlayer *player = [AVPlayer playerWithPlayerItem:item]; [player play]; break; } case AVAssetExportSessionStatusFailed: NSLog(@"导出失败:%@", exportSession.error); break; default: break; } }]; ``` 另一种方法是使用 AVAssetResourceLoaderDelegate,通过响应 NSURLSessionDataTask 的回调来实现音频下载。具体步骤如下: 1. 创建 AVURLAsset 对象,设置其 resourceLoader 的代理为自定义的 AVAssetResourceLoaderDelegate 对象。 2. 在代理对象实现 resourceLoader:shouldWaitForLoadingOfRequestedResource: 方法,该方法会在播放器请求某个资源时被调用。 3. 在该方法,创建一个 NSURLSessionDataTask 对象,使用该对象下载资源,并将下载完成的数据通过 AVAssetResourceLoadingRequest 的 respondWithData: 方法返回给播放器。 4. 在代理对象实现 resourceLoader:didCancelLoadingRequest: 方法,该方法会在播放器不再需要请求某个资源时被调用,可以在该方法取消 NSURLSessionDataTask 对象。 示例代码如下: ```objc @interface CustomAVAssetResourceLoaderDelegate : NSObject<AVAssetResourceLoaderDelegate> @property (nonatomic, strong) NSURLSession *session; @end @implementation CustomAVAssetResourceLoaderDelegate - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest { // 创建 NSURLSessionDataTask 对象,下载资源 NSURLSessionDataTask *task = [self.session dataTaskWithURL:loadingRequest.request.URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error) { [loadingRequest finishLoadingWithError:error]; } else { // 将下载完成的数据返回给播放器 [loadingRequest.dataRequest respondWithData:data]; [loadingRequest finishLoading]; } }]; [task resume]; return YES; } - (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest { // 取消 NSURLSessionDataTask 对象 [self.session invalidateAndCancel]; } @end // 获取音频资源 AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:@"http://example.com/audio.mp3"]]; // 设置 resourceLoader 的代理为自定义的 AVAssetResourceLoaderDelegate 对象 CustomAVAssetResourceLoaderDelegate *delegate = [[CustomAVAssetResourceLoaderDelegate alloc] init]; delegate.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [asset.resourceLoader setDelegate:delegate queue:dispatch_get_main_queue()]; // 使用 AVPlayer 播放音频 AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset]; AVPlayer *player = [AVPlayer playerWithPlayerItem:item]; [player play]; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值