Strange behavior: save video recorded within app?
- by Josue Espinosa
I allow the user to record a video within my app, then later play it again. When a user records a video, I save the URL of the video, then play the video later from the saved URL. I save the video both in the Photos app and in my app. If I delete the video within the photos app, it still plays. After about 7 days, the video gets deleted. I think I am saving in my tmp directory, but i'm not sure. Here is what I am doing:
-(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]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];
NSString *dateAdded = [dateFormatter stringFromDate:date];
[video setDate_recorded:dateAdded];
if(_currentAthlete != nil){
[video setWhosVideo:_currentAthlete];
}
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);
}
}
}
Am I saving it incorrectly? When I go to play the video, it works fine, after a couple days the video will play without audio, then eventually it will be gone. Any ideas why?