在我的应用程序中,我想从服务器下载视频文件并将该文件存储在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配合使用。

如果有任何示例代码意味着它对我非常有帮助。

谢谢!!!

最佳答案

 -(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/

10-14 21:59
查看更多