仅打开图书馆的视频

时间:2015-07-09 09:20:04

标签: ios objective-c iphone xcode

我有两个按钮来获取图像并从设备获取视频。 对于图像按钮,我这样编码

- (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;}

在此代码中仅显示图像.. 但是当我按btn_video时,我怎么才能看到来自设备的视频而没有图像。 我不想从相机录制视频。  请给我代码..

1 个答案:

答案 0 :(得分:1)

将此行放在按钮代码中:

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

并添加Frameworks MobileCoreService

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);
            }
        }
}

视频委托方法:

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