2013-10-20 40 views
1

我允許用戶在我的應用內錄製視頻,然後再播放一次。當用戶錄製視頻時,我保存視頻的網址,然後從保存的網址播放視頻。我將這些視頻都保存在照片應用和我的應用中。如果我在照片應用程序中刪除了視頻,它仍會播放。大約7天后,視頻被刪除。我想我正在保存在我的tmp目錄中,但我不確定。這是我正在做的事:奇怪的行爲:保存在應用程序內記錄的視頻?

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 


    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    // Handle a movie capture 
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) { 
     NSString *moviePath = [NSString stringWithFormat:@"%@",[[info objectForKey:UIImagePickerControllerMediaURL] path]]; 
     NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
     NSData *videoData = [NSData dataWithContentsOfURL:videoURL]; 
     _justRecordedVideoURL = [NSString stringWithFormat:@"%@",videoURL]; 

     AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
     _managedObjectContext = [appDelegate managedObjectContext]; 
     Video *video = [NSEntityDescription insertNewObjectForEntityForName:@"Video" inManagedObjectContext:_managedObjectContext]; 

     [video setVideoData:videoData]; 
     [video setVideoURL:[NSString stringWithFormat:@"%@",videoURL]]; 

     NSError *error = nil; 
     if(![_managedObjectContext save:&error]){ 
      //handle dat error 
     } 


     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"]; 

     BOOL success = [videoData writeToFile:tempPath atomically:NO]; 

     if(success == FALSE){ 
      NSLog(@"Video was not successfully saved."); 
     } 

     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) { 
      UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, 
               @selector(video:didFinishSavingWithError:contextInfo:), nil); 

     } 
    } 
} 

我可是不能正常保存它?當我開始播放視頻時,它可以正常工作,幾天後視頻將播放而不需要音頻,最終它將消失。任何想法爲什麼?

回答

1

核心數據編程指南明確指出,你不應該保存在覈心數據過大的斑點。縮略圖沒問題,應該避免使用大圖像或視頻。

我會建議在存儲核心數據,例如在當地視頻網址具有唯一的名稱,並將視頻存儲在文檔目錄中。現在,您首先將其保存在Core Data中,然後將核心數據屬性寫入文件。最好將數據直接寫入本地文件。

你應該儘量簡化你的代碼。你有幾乎相同的變量存儲很多次沒有明顯的原因(同一URL的URL例如和字符串變量:moviePathvideoURL_justRecordedVideoURL)。

模式[NSString [email protected]"%@", string]沒有意義。
模式[NSString [email protected]"%@", url]最好寫成[url path]