我正在开发一个名为 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];
}
视频未在滚动视图中显示。但是当单击视频文件时,它正在导航。当图像设置为按钮时,图像显示在视图中。提前致谢。