如何使用核心数据导入视频?

时间:2012-09-16 10:14:22

标签: objective-c ios xcode

我正在开发一个名为safekeep的应用程序。我必须通过转换为数据来使用coredata导入视频。

if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.movie"]) 
{
     NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
     NSLog(@"Q: video path: %@",[videoURL description]);

     self.videoData=[NSData dataWithContentsOfURL:videoURL];
     if(self.videoData)
      {
        NSLog(@"data is present");
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        NSManagedObject *imagetblObj= [NSEntityDescription insertNewObjectForEntityForName:@"VideoData" inManagedObjectContext:context];
        [imagetblObj setValue:self.videoData forKey:@"videodata"];
        [imagetblObj setValue:str forKey:@"date"];
        NSError *err;
        if (![context save:&err]) 
         {
            NSLog(@"Couldn't save history item into coredata");
         }

        NSLog(@"data saved");
        [self videodatafromdb];
    }
}

-(void)videodatafromdb
{
    NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"VideoData" inManagedObjectContext:context];
    [request setEntity:entity];
NSError *error;
NSArray *recordsData=[context executeFetchRequest:request error:&error];
self.videoarray=[[recordsData reverseObjectEnumerator]allObjects];
NSLog(@"Array count is %d",[self.videoarray count]);
if ([self.videoarray count]>0) 
{
  [self createScrollViewvideo];
}
}

-(void)createScrollViewvideo
{
    NSLog(@"in create scrollviewvideo");
    //add views to scrolview
    int x=5;
    int y=17;
    for(int i=0;i<[videoarray count];i++)
    {
        UIImage *imag=[[UIImage alloc] init];
        UIView *videoview=[[UIView alloc] initWithFrame:CGRectMake(x, y, 100, 100)];
        videoview.tag=i;

        UIButton *userButton=[[UIButton alloc]initWithFrame:CGRectMake(1, 1, 100,100)];
        [userButton addTarget:self action:@selector(userVideoClicked:) forControlEvents:UIControlEventTouchUpInside];
        userButton.tag=i;
        imag=[UIImage imageWithData:videoData];

        [userButton setBackgroundImage:imag forState:UIControlStateNormal];

        [videoview addSubview:userButton];
        [self.scrollview addSubview:videoview];


        [userButton release];
        [videoview release];

        x+=104;
        if ((i+1)%3==0) 
        {
            y+=110;
            x=5;
        }

    }
    NSLog(@"10000in create scrollview");
    if (y+110>self.scrollview.frame.size.height) 
    {
        self.scrollview.contentSize=CGSizeMake(320, y+110);
    }
    else
    {
        self.scrollview.contentSize=CGSizeMake(320, self.scrollview.frame.size.height+60);
    }
 }
}

- (IBAction为)userVideoClicked:(ID)发送方 {     NSLog(@“视频点击”);

UIButton *button=(UIButton*)sender;

VideoData *videoobj=(VideoData *)[self.videoarray  objectAtIndex:[button tag]];

NSLog(@"video data is %@",videoobj);

VidoesVIewController *videoviewcontroller=[[VidoesVIewController alloc] initWithNibName:@"VidoesVIewController" bundle:nil];

videoviewcontroller.videodata=[videoobj valueForKey:@"videodata"];

[self.navigationController pushViewController:videoviewcontroller animated:YES];

[videoviewcontroller release];

}

视频未显示在滚动视图中。但是当点击视频文件时,它正在导航。当图像设置为按钮时,图像显示在视图中。提前感谢。

1 个答案:

答案 0 :(得分:0)

请参阅我的回答:

Can I access the files used for external binary storage in Core Data?

我提出了一个“创造性”解决方案(无论是最好的做法都是猜测),但它允许您使用Core Data管理文件,同时仍然可以通过路径访问REAL文件。

通过在二进制字段上使用“允许外部存储”选项,您可以允许核心数据为您管理对文件的引用,从而抽象出相当数量的繁忙工作。