2010-06-08 63 views
3

我正在使用UIImagePickerController,它使用戶能夠選擇現有照片或使用相機拍攝當時的圖像。我可以在UIImageView的應用程序中顯示該圖像。如何將視頻或電影文件顯示到UIImagePickerController中?

現在我想爲電影也使用這種能力。但我無法找到任何方式在我的應用中將選定的電影顯示爲圖像,就像照片應用一樣。如您所知,您可以在同一個列表中看到照片和電影。

+1

你爲什麼要添加這些問題的答案?如果你想包括代碼,把它放到問題中(並請將其格式化爲代碼 - 這是你現在這樣做的方式非常不可讀) – 2010-06-09 07:42:49

回答

2
-(IBAction) selectImageSelected : (id)sender 
{ 
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image" 
              delegate:self 
            cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
            otherButtonTitles:@"Take Picture", @"Select from gallery", nil]; 
    [actionSheet showInView:self.parentViewController.tabBarController.view]; 
} 

- (void)actionSheet:(UIActionSheet *)_actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if(buttonIndex==0) 
    { 
     if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 
      [AlertHandler showAlertMessage:[ErrorMessages getCameraNotFoundMsg] withTitle:@"No Camera Detected"]; 
      return; 
     } 
     else 
     { 
      imagePicker = [[UIImagePickerController alloc] init]; 
      imagePicker.delegate = self; 
      imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
      [self presentModalViewController:imagePicker animated:YES]; 
     } 
    } 
    else if(buttonIndex==1) 
    { 
     imagePicker = [[UIImagePickerController alloc] init]; 
     imagePicker.delegate = self; 
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType]; 
     [self presentModalViewController:imagePicker animated:YES]; 
    } 
    else 
    { 
     [actionSheet dismissWithClickedButtonIndex:2 animated:YES]; 
    } 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 

    if ([mediaType isEqualToString:@"public.image"]){ 

     // UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 

     UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

     NSLog(@"found an image"); 

//  [UIImageJPEGRepresentation(image, 1.0f) writeToFile:[self findUniqueSavePath] atomically:YES]; 

//  SETIMAGE(image); 

     CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]); 

    } 

    else if ([mediaType isEqualToString:@"public.movie"]){ 

     NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 

     NSLog(@"found a video"); 

     NSData *webData = [NSData dataWithContentsOfURL:videoURL]; 

     //NSData *video = [[NSString alloc] initWithContentsOfURL:videoURL]; 

//  [webData writeToFile:[self findUniqueMoviePath] atomically:YES]; 

     CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]); 
     CGSize sixzevid=CGSizeMake(imagePicker.view.bounds.size.width,imagePicker.view.bounds.size.height-100); 
     UIGraphicsBeginImageContext(sixzevid); 
     [imagePicker.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     eventButton.imageView.image=viewImage; 
     // NSLog(videoURL); 

    } 

    [picker dismissModalViewControllerAnimated:YES]; 

} 

/* 
- (void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo1 
{ 
    [imagePicker dismissModalViewControllerAnimated:YES]; 
    imagePicker.view.hidden = YES; 
    eventButton.imageView.image = image; 
    imageSelected = YES; 
} 
*/ 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ 
    [imagePicker dismissModalViewControllerAnimated:YES]; 
    imageSelected = NO; 
} 
-1

要與只有電影文件

選擇器視圖替換

imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType]; 

picker.mediaTypes = [NSArray arrayWithObject:@"public.movie"]; 
相關問題