0

我正在开发一个名为 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)sender { NSLog(@"video clicked");

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

}

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

4

1 回答 1

0

请参阅我的答案:

我可以访问 Core Data 中用于外部二进制存储的文件吗?

我想出了一个“创造性”的解决方案(无论它是最佳实践是否有人猜测),但它允许您使用 Core Data 来管理您的文件,同时仍然能够通过它们的路径访问真实文件。

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

于 2012-11-21T17:17:53.097 回答