将视频URL保存到Core Data

时间:2013-12-01 05:05:46

标签: ios objective-c cocoa-touch uiimagepickercontroller

在我的应用中,我利用UIImagePickerController录制视频。然后我将视频URL从相机胶卷保存到核心数据中,每当我想播放它时,我都会拉URL并这样做。但是,当从照片应用中删除视频时,它仍会播放几天。当它没有从照片应用程序中删除时,它仍然会在几天后删除。如何将视频保存到我的应用程序文档中,并将其保存到核心数据中?这是我目前使用的(它不起作用):

以下是无效的zip文件:http://jmp.sh/v/w4gE5SXNiRd0d7tasc3U

以下是现在可以正常运行的zip文件:http://jmp.sh/v/tzyJU3nlc1qOPI9ZzDTF

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
 NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
 // 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];

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

 [video setVideoData:videoData];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 int random = arc4random() % 1000;
 //[documentsDirectory stringByAppendingFormat:@"/vid1.mp4"]
 NSString *tempPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"vid%d%@%@.mp4", random, self.currentAthlete.first, self.currentAthlete.last]];
 BOOL success = [videoData writeToFile:tempPath atomically:NO];
 if (success == FALSE) {
 NSLog(@"Video was not successfully saved.");
 }
 else{
 [video setVideoURL:[NSString stringWithFormat:@"%@", tempPath]];
 }

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

 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
 UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
 @selector(video:didFinishSavingWithError:contextInfo:), nil);
 }
 }
 [self dismissViewControllerAnimated:YES completion:nil];
 }

3 个答案:

答案 0 :(得分:5)

核心数据本身。

等等,在您声称不应该直接将大型二进制对象存储到Core Data Store之前,我同意。你不应该。

但是,如果文件很大,Core Data中有一个选项可以使用外部存储来存储二进制数据。

enter image description here

现在,如果文件很大,Core Data将负责保存文件并存储URL。

保存您管理数据文件。

答案 1 :(得分:3)

不要创建这样的路径:

NSString *tempPath = [NSString stringWithFormat:@"%@/vid%d%@%@.mp4", documentsDirectory, random, self.currentAthlete.first, self.currentAthlete.last];

像这样创建:

NSString *tempPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"vid%d%@%@.mp4", random, self.currentAthlete.first, self.currentAthlete.last]];

答案 2 :(得分:0)

此代码将NSData对象写入文档目录(您可以更喜欢其他位置)并指定其URL,然后将相同的URL保存到核心数据。

(H)

    @property (nonatomic, retain) NSData *data;

(。m)

    NSString *docsDir;
    NSArray *dirPaths;

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:[@"video_" stringByAppendingString:[NSString stringWithFormat: @"%d.mp4", 0]]]];

    [self.data writeToFile:databasePath atomically:YES];
    NSURL *videoURL = [NSURL fileURLWithPath:databasePath];

    if(videoURL)
    {
        //save url to core data 
    }