应用程序在下载视频时崩溃了

时间:2011-11-19 07:44:27

标签: iphone objective-c

在我的应用程序中使用NSURLConnection下载一些视频。它显示了两个内存警告。

          Received memory warning. Level=1
          Received memory warning. Level=2

然后经过一段时间App崩溃有什么方法可以解决这个问题。 任何人都可以帮我解决这个问题。

提前致谢。

我使用的代码:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:[exer1 objectAtIndex:i-1]]cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];
     NSLog(@"%@ urlrequest ",theRequest);


        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

        HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];

        HUD.labelText =[NSString stringWithFormat:@"Downloading %d Video",[exer1 count]-(i-1)];

        if (theConnection) {
            // Create the NSMutableData to hold the received data.
            // receivedData is an instance variable declared elsewhere.
            receivedData = [[NSMutableData data] retain];

        } else {
            // Inform the user that the connection failed.
        }

        }

    }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    expectedLength = [response expectedContentLength];
    currentLength = 0;
    HUD.mode = MBProgressHUDModeDeterminate;

    NSLog(@"%d expectedLength",expectedLength);

    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.

    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.

    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    currentLength += [data length];
    HUD.progress = currentLength / (float)expectedLength;
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];

}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [HUD hide:YES];
    // release the connection, and the data object
    // [connection release];
    // receivedData is declared as a method instance elsewhere
    // [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

    if(tempz == 1){

        tempz =0;
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"The Internet connection appears to be offline." message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }



}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *err;
    NSFileManager *fman = [NSFileManager defaultManager];

    NSString *path = [[[fman URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
    NSDictionary *fattr = [fman attributesOfFileSystemForPath:path error:&err];
    //Error checking 
    NSUInteger freeSize = [[fattr objectForKey:NSFileSystemFreeSize] unsignedIntegerValue];

    NSLog(@"Free Space %d", freeSize);

    NSArray* paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    struct statfs tStats;  
    statfs([[paths1 lastObject] cString], &tStats);  
    float total_space = (float)(tStats.f_blocks * tStats.f_bsize);  


    NSLog(@"%f total_space",total_space);



        [SavedVid addObject:[exer1 objectAtIndex:i-1]];
        NSUserDefaults *currentDefaults6 = [NSUserDefaults standardUserDefaults];
        [currentDefaults6 setObject:[NSKeyedArchiver archivedDataWithRootObject:SavedVid] forKey:@"SavedVid"];
        [currentDefaults6 synchronize];


       NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"] ;


  /*  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSLog(@"%@ documentsDirectory",documentsDirectory);
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
    }

    */


    NSString *file2 = [NSString stringWithFormat:@"%@/%@.mp4",cacheDirectory,[elementw.description  objectAtIndex:i-1]];


   /* NSLog(@"%@ documentsDirectory",file2);
      NSLog(@"%@ ---- i elementw.description   ---- %d ",elementw.description,i);
  */
    [receivedData writeToFile:file2 atomically:YES];

   NSLog(@" %@ file 2 ",file2);

    HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tick.png"]] autorelease];
    HUD.mode = MBProgressHUDModeCustomView;
    [HUD hide:YES afterDelay:2];


    [self initializPlayer];

    // release the connection, and the data object
   // [connection release];
 //   [receivedData release];
}

2 个答案:

答案 0 :(得分:1)

收到数据后,您需要将其存储在临时(或不那么临时)的文件中。问题是整个文件正在下载到内存中,这会占用大部分内存。

应用程序崩溃是因为它占用了太多内存,因此写入文件是最佳选择。

这样的事情:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    currentLength += [data length];
    HUD.progress = currentLength / (float)expectedLength;

    NSString *temporaryFile = [NSString stringWithFormat:@"%@/fileDownload.part", NSTemporaryDirectory()];

    if (![[NSFileManager defaultManager] fileExistsAtPath:temporaryFile]) {
        [[NSFileManager defaultManager] createFileAtPath:temporaryFile contents:nil attributes:nil];
    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:temporaryFile];
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:data];
    [fileHandle closeFile];

    if (currentLength == expectedLength) {
        //read from the file stored at temporaryFile
        //perhaps move to a permanent location and change
        //its file name and extension.
    }

}

答案 1 :(得分:0)

我认为你将视频保留在你的记忆中。根据大小,它可能会导致内存问题。