我允許用戶在我的應用內錄製視頻,然後再播放一次。當用戶錄製視頻時,我保存視頻的網址,然後從保存的網址播放視頻。我將這些視頻都保存在照片應用和我的應用中。如果我在照片應用程序中刪除了視頻,它仍會播放。大約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);
}
}
}
我可是不能正常保存它?當我開始播放視頻時,它可以正常工作,幾天後視頻將播放而不需要音頻,最終它將消失。任何想法爲什麼?