在我的应用程序中,我想从服务器下载视频文件并将该文件存储在IPhone文档目录中。
下载我正在使用ASIHTTPRequest库,我已经下载了视频(KB大小)并存储到Document目录中。
但是,当我下载视频(以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。
如果任何示例代码对我有帮助。
感谢!!!
答案 0 :(得分:6)
-(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);
}
您将看到该文件位于桌面上。
享受!