在我的应用程序中,我想从服务器下载视频文件并将该文件存储在 iPhone 文档目录中。
对于下载,我使用 ASIHTTPRequest 库,并且我已经下载了视频(以 KB 大小)并存储到文档目录中。
但是,当我下载视频(以 MB 大小为单位)时,意味着该请求需要更多时间来下载视频,并且没有结果(我已经等待了 15 分钟以上,但没有下载)。
我已在我的应用程序中尝试了以下代码,这是从服务器进行的单个下载。
- (IBAction)grabURLInBackground:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *url = [NSURL URLWithString:@"http://mobile.aghaven.com/Downloads/video/Movie.m4v"];
//NSString *file = [NSString stringWithFormat:@"%@/movie.mov", documentsDirectory];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSString* file = [documentsDirectory stringByAppendingPathComponent:@"Movie.m4v"];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadDestinationPath:file];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
[[[[UIAlertView alloc] initWithTitle:@"Message"
message:@"Success!!!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
此文件(Movie.m4v)大小为 2.2 MB,当我从服务器下载此视频时,意味着没有下载任何视频。
请建议我如何使用 ASIHTTPRequest 从服务器下载视频文件(MB 大小)?
我正在使用 XCode 4.2 和 iOS 5 sdk。
如果有任何示例代码意味着它对我非常有帮助。
谢谢!!!
最佳答案
-(void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://mobile.aghaven.com/Downloads/video/Movie.m4v"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/pk/Desktop/my_file.m4v"];
[request setDelegate:self];
[request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
[[[[UIAlertView alloc] initWithTitle:@"Message"
message:@"Success!!!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease] show];
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"%@",request.error);
}
您将看到该文件位于桌面上。
享受吧!
关于iphone - 使用 ASIHTTPRequest 库从服务器下载视频文件(MB 大小),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635491/