【问题标题】:iOS - Download VideoiOS - 下载视频
【发布时间】:2013-07-21 06:19:33
【问题描述】:

我想从远程 URL 下载视频并将其保存到 iPhone 应用中的文件中。 我知道视频链接有效,因为我已经从 AVPlayer 使用过它,但是我无法下载它。响应始终为 (null)。

以下代码有什么问题?

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:someURLString]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:someFilePath append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:someFilePath]);
        NSLog(@"THE RESPONSE: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation start];

更新

我注释掉了 operation.outputStream 这一行,这次我得到了回应。这是否意味着文件路径有问题?

【问题讨论】:

  • 文件下载了吗?
  • 不,操作总是“成功”,但没有下载任何东西@H2CO3
  • 您是否尝试将其下载到应用程序包中?
  • 不,到文档目录@H2CO3 中的一个文件夹
  • 查看更新后的问题,@H2CO3

标签: iphone ios objective-c cocoa-touch afnetworking


【解决方案1】:

只需创建指向该文件的链接,然后使用 NSURLConnection 下载。

创建一个 URL 连接进行下载:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:strFileUrl]]; //strFileURL is url of your video/image
NSURLConnection *conection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[conec start];
[request release];

获取保存数据的文件路径:

strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strFileName];

你的类必须采用 NSURLConnectionDelegate 协议的 3 种方法:(请阅读 Protocol 和 Delegate)

   - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        // create 
        [[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
        file = [[NSFileHandle fileHandleForUpdatingAtPath:strFilePath] retain];// read more about file handle
        if (file)   {
            [file seekToEndOfFile];
        }
    }



 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
    {    
        //write each data received
        if( receivedata != nil){
            if (file)  { 
                [file seekToEndOfFile];
            } 
            [file writeData:receivedata]; 
        }
    }

    - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
        //close file after finish getting data;
        [file closeFile];
    }

    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        //do something when downloading failed
    }

如果您想查看文件,请使用 UIWebview 加载它:

 NSURL *fileURL = [NSURL fileURLWithPath:strFilePath];
    [wvReview loadRequest:[NSURLRequest requestWithURL:fileURL]];

【讨论】:

    【解决方案2】:

    这就是问题所在。

    我使用的是视频的 url,并作为文件路径的一部分。

    为什么会这样?它有反斜杠,所以我认为 iOS 会感到困惑。

    经验教训:确保添加到目录以创建文件的字符串没有反斜杠。

    我希望这可以帮助其他犯此愚蠢错误的人。 :P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-15
      • 2014-11-26
      • 2015-01-20
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多