2011-12-26 57 views
2

在我的应用程序中,我想从服务器下载视频文件并将该文件存储在IPhone文档目录中。使用ASIHTTPRequest库从服务器下载视频文件(以MB为单位)

对于下载,我使用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]; 
} 

这个文件中有2.2 MB大小,当我下载从服务器表示这部影片(Movie.m4v),没有视频下载。

请建议我如何使用ASIHTTPRequest?从服务器下载视频文件(以MB为单位)。

我正在使用iOS 5 sdk的XCode 4.2。

如果有示例代码对我非常有帮助。

谢谢!

+1

尝试使用进度var来检查它何时停止下载。只是为了测试目的注释掉设置的超时时间,然后再试一次。加上尝试下载超过2 MB的图像... – doNotCheckMyBlog 2011-12-26 12:08:41

+0

只需要注意,ASI不再被维护。您应该尝试使用AFNetworking。 – 2011-12-26 12:53:42

+0

很多谢谢dontCheckMyBlog,雅各布Relkin ..我会考虑你的建议.. – user2136 2011-12-27 02:45:39

回答

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); 

} 

你会看到该文件在桌面上。

享受!

+0

谢谢哈迪克沙阿!! ..我会检查你的代码和视频文件(Movie.m4v)有2MB大小,是否有可能下载和将它存储在IPhone文档目录中?..我想将视频文件存储在IPhone设备中,一旦它下载了。期待你的回答... – user2136 2011-12-27 02:49:25

+0

是的,当然,因为你必须将downloaddestinationpath指向NSDocumentDirectionary。有很多代码可用于在文档目录中保存文件。 – 2011-12-27 05:54:18

+0

是的谢谢!我已经搞清楚了.... – user2136 2011-12-27 08:03:55

相关问题