How to show video or movie in UIImagePickerController?

I use UIImagePickerController, which gives the user the ability to select an existing photo or use the camera to take an image at this time. And I can show this image in my application with UIImageView.

Now I want to use this opportunity for films. But I could not find a way to show the selected movie as an image in my application, as the Photos application. as you know, you can see photos and films in the same list.

+3
source share
3 answers
-(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;
}
+2
source

To have an idea of ​​choice with only movie files

replace

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

with

picker.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
-1
source

Source: https://habr.com/ru/post/1749002/


All Articles