0

Bellow 我有一些代码可以完成 youtube 视频的下载过程。它工作一次,然后第二次不工作,所以我把这行代码放在[videoData retain];其中停止我收到的线程错误。但是,一旦添加[videoData retain];我的下载器,下载第二个视频时下载两次而不是一次,下载第一个视频时不会这样做...

这是我的代码:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        videoData = [[NSMutableData alloc] init];

    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [videoData appendData:data];

    }

    //when downloading done
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        filePath = [NSString stringWithFormat:@"%@/Documents/%f.mp4", NSHomeDirectory(),[[NSDate date] timeIntervalSince1970]];
        [videoData writeToFile:filePath atomically:YES];
        [videoData retain];

        [videoData release];


        NSLog(@"LOCAL VIDEO LINK %@",filePath);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Downloading complete"
                                                       message: @"Download complete. You have successfully downloaded your video from youtube"
                                                      delegate: self
                                             cancelButtonTitle:@"Go Back"
                                             otherButtonTitles:@"OK",nil];

        [alert show];
        [alert release];


    }
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
            NSLog(@"user pressed Button Indexed 0");
            // Any action can be performed here
            [movieController stop];
            [movieController.view removeFromSuperview];
            bar.hidden = YES;
            texturl.text = nil;
            hidden.hidden = YES;
            videoData = nil;




        }
        else
        {
            NSLog(@"user pressed Button Indexed 1");
            // Any action can be performed here
        }
    }

提前致谢

4

0 回答 0