将视频保存到文档而不是相册中

时间:2013-09-01 20:09:47

标签: ios objective-c cocoa-touch video avfoundation

目前,我的用户录制了一段视频,并将其保存到iPad的相册中。相反,我想将我的应用程序录制的视频存储在我的应用程序目录中并保存。后来我希望能够播放我在应用中录制的所有视频。这是我到目前为止所得到的:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:NO];
    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
                                                @selector(video:didFinishSavingWithError:contextInfo:), nil);
            NSData *movieData = [NSData dataWithContentsOfURL:moviePath];
            //confused on what to do after I have the movie data.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

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

    NSLog(@"%hhd",success);

        }
    }
}

注意:我只录制视频,没有图片,如果重要的话。

1 个答案:

答案 0 :(得分:3)

#pragma mark -
#pragma mark File Names and Paths
// Creates the path if it does not exist.
- (void)ensurePathAt:(NSString *)path {
    NSFileManager *fm = [NSFileManager defaultManager];
    if ( [fm fileExistsAtPath:path] == false ) {
        [fm createDirectoryAtPath:path
      withIntermediateDirectories:YES
                       attributes:nil
                            error:NULL];
    }
}
- (NSString *)documentPath {
    if ( ! documentPath_ ) {
        NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentPath_ = [searchPaths objectAtIndex: 0];
        documentPath_=[documentPath_ stringByAppendingPathComponent:@"VideoAlbum"];
        [documentPath_ retain];
    }
    return documentPath_;
}

- (NSString *)audioPath {
    if ( ! AudioPath_ ) {
        AudioPath_ = [[self documentPath] stringByAppendingPathComponent:@"Demo"];
        NSLog(@"%@",AudioPath_);
        [AudioPath_ retain];
        [self ensurePathAt:AudioPath_];
    }
    return AudioPath_;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
    {
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        tempPath = [[self audioPath] stringByAppendingFormat:@"/%@.mp4",[NSDate date]];
        BOOL success = [videoData writeToFile:tempPath atomically:NO];

        NSLog(@"%hhd",success);
    }
    [[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}