简体   繁体   中英

Strange behavior: save video recorded within app?

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]];

        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?

The Core Data programming guide specifically points out that you should not save too large blobs in Core Data. Thumbnails are OK, large images or vides should be avoided.

I would recommend storing the local video URL in Core Data, eg with a unique name, and storing the video in the documents directory. Now, you are first saving in Core Data and then writing the core data attribute to a file. It is preferable to write the data to the local file directly.

You should try to simplify your code. You have practically the same variable stored many times for no apparent reason (eg URL and string variants of the same URL: moviePath , videoURL , _justRecordedVideoURL ).

The pattern [NSString stringWithFormat@"%@", string] does not make sense.
The pattern [NSString sringWithFormat@"%@", url] is better written as [url path] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM