如何使用目标c中的代码保存视频? [英] How to save a video using code in objective c?

查看:186
本文介绍了如何使用目标c中的代码保存视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用目标c捕获了视频,但是我无法将其保存在iphone照片库中.我不想使用AlAssets库,我只想使用图像选择器.我已经看到了很多堆栈溢出和其他站点上的方法,但它们要么使用存储位置路径(未提及它是什么),要么不起作用. 这是我的代码.

I have captured a video using objective c,however I am not able to save it in the iphone photos library.I dont want to use the AlAssets library,I want to use the image picker only.I have seen a lot of methods on stack overflow and other sites but they either use storage location path(which is not mentioned what it is) or they dont work. This is my piece of code.

-(IBAction)Onclick:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController

                                             alloc] init];
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes =  [NSArray arrayWithObject:(NSString *)

                               kUTTypeMovie];
    imagePicker.delegate = self;
    //UISaveVideoAtPath
    imagePicker.allowsImageEditing = NO;
    [self.view addSubview:imagePicker.view];
    [imagePicker viewWillAppear:YES];

    CGRect overlayFrame = CGRectMake(0, 380, 320, 44);
    //UILabel *lbl=[[UILabel alloc]init];
    UIView *baseView = [[[UIView alloc] init] autorelease];
    baseView.backgroundColor =[UIColor greenColor];
    //lbl.text=@"dfgfdgfd";
    baseView.frame=overlayFrame;
    //view.delegate = self;
    //view.picker = picker;
    //[view customize];
    imagePicker.cameraOverlayView = baseView;


    [self presentModalViewController:imagePicker animated:YES]; 


}


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


    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:@"public.image"]){
        UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
        UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil);
    }
    else if ([mediaType isEqualToString:@"public.movie"]){

        NSURL *url = [[[info objectForKey:UIImagePickerControllerMediaURL] copy] autorelease];

//        ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
//        [library writeVideoAtPathToSavedPhotosAlbum:url
//                                    completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}];
UISaveVideoAtPathToSavedPhotosAlbum(url, nil, nil, nil);
    }

    [self dismissModalViewControllerAnimated:YES];

}

推荐答案

尝试一下:

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

         [self dismissViewControllerAnimated:YES completion:nil];

        // Handle a movie capture
        if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0)
            == kCFCompareEqualTo)
        {

            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:@"Photo/Video Saving Failed"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo/Video Saved" message:@"Saved To Photo Album"  delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}

希望它对您有帮助.

这篇关于如何使用目标c中的代码保存视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆