-1

I have two button to get images and to get videos from device. For Image button i code like this

- (IBAction)btn_image:(id)sender{
UIImagePickerController *img=[[UIImagePickerController alloc]init];
img.delegate=self;
img.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:img animated:YES completion:nil];}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *img=info[UIImagePickerControllerOriginalImage];
imgview.image=img;}

In this code shows only images.. but when i press btn_video then how can i see only videos from device and no images. I don't want to record video from camera. please give me code..

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
  • possible duplicate of [How to select any Video or Movie file from UIImagePickerController](http://stackoverflow.com/questions/10480170/how-to-select-any-video-or-movie-file-from-uiimagepickercontroller) – Droppy Jul 09 '15 at 09:26

1 Answers1

1

put this line in your button code :

UIImagePickerController *img = [[UIImagePickerController alloc] init];
        img.delegate = self;
        img.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        img.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
        [self presentModalViewController:imagePicker animated:YES];

and Add Frameworks MobileCoreService

get Video from didFinishPickingMediaWithInfo :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

        // Handle a movie capture
        if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
        {
            [self dismissViewControllerAnimated:YES completion:Nil];
            NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
            {
                UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,@selector(video:didFinishSavingWithError:contextInfo:), nil);
            }
        }
}

video delegate method :

-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
    if (error)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
  • I always find the best way to see if a question is a duplicate is to search for any code you add to your answers as you constantly use other people's answer without attribution... – Droppy Jul 09 '15 at 09:28
  • its my own answer not copyed anywhere @Droppy – Jay Bhalani Jul 09 '15 at 09:30
  • 2
    I implement that code but my app terminated. `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'No available types for source 0'` – Bhavin Ramani Jul 09 '15 at 09:32
  • yeah that code worked.. but when i click on video it play it.but i dont want to play the video i want to show it in view as like image... – Bhavin Ramani Jul 09 '15 at 09:39
  • when i choose a video a view appear and there is a choose button on pressing this it compressing what happened that @Jay Bhalani – Bhavin Ramani Jul 09 '15 at 09:42
  • No i dont Want to play video i want upload that video on server – Bhavin Ramani Jul 09 '15 at 09:44
  • you can get video from this method , See my edited answer @bhavinramani – Jay Bhalani Jul 09 '15 at 09:49
  • got error on `@selector(video:didFinishSavingWithError:contextInfo:)` – Bhavin Ramani Jul 09 '15 at 09:52
  • on this code `NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];` the error is `/Users/mymac/Desktop/single image/single image/ViewController.m:43:19: Incompatible pointer types initializing 'NSString *__strong' with an expression of type 'CGPathRef' (aka 'const struct CGPath *')` – Bhavin Ramani Jul 09 '15 at 09:56
  • I want to saw that selected video to my view.not to save anywhere – Bhavin Ramani Jul 09 '15 at 09:58