系统相册,相机,拍视频

#import //资产库框架

#import //移动核心服务框架




@property(nonatomic,strong)UIImagePickerController *picker;


-(void)tuxiang

{

    //初始化图像选择控制器

    self.picker = [[UIImagePickerController alloc]init];

    //遵守代理

    self.picker.delegate =self;

}


-(void)paizhao

{

    //如果拍摄的摄像y头可用

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // 将sourceType设为UIImagePickerControllerSourceTypeCamera代表拍照或拍视频

        self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        //设置拍摄照片

        self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

        //设置使用手机的后置摄像头(默认使用后置摄像头)

        self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;

        //设置使用手机的前置摄像头。

        //picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;

        // 设置拍摄的照片允许编辑

        _picker.allowsEditing =YES;

    }else{

        NSLog(@"模拟器无法打开摄像头");

    }

    // 显示picker视图控制器

    [self presentViewController:self.picker animated:YES completion:nil];

}

-(void)shipin

{

    //如果拍摄的摄像头可用

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        // 将sourceType设为UIImagePickerControllerSourceTypeCamera代表拍照或拍视频

        self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        // 将mediaTypes设为所有支持的多媒体类型

        self.picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

        //设置拍摄视频

        self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;

        //设置拍摄高质量的视频

        self.picker.videoQuality = UIImagePickerControllerQualityTypeHigh;

    }else{


        NSLog(@"模拟器无法打开摄像头");

    }

    // 显示picker视图控制器

    [self presentViewController:self.picker animated:YES completion:nil];

}

-(void)xiangce

{

    //设置选择载相册的图片或视频

    self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    //是否允许编辑

    self.picker.allowsEditing = NO;

    //显示picker视图控制器

    [self presentViewController:self.picker animated:YES completion:nil];

}

#pragma mark - 图像选择控制器代理实现

// 当得到照片或者视频后,调用该方法

-(void)imagePickerController:(UIImagePickerController*)picker

didFinishPickingMediaWithInfo:(NSDictionary*)info

{

    //获取用户拍摄的是照片还是视频

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

   //判断获取类型:图片,并且是刚拍摄的照片

    if ([mediaType isEqualToString:(NSString*)kUTTypeImage] && picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImage* theImage =nil;

      //判断图片是否允许修改

        if([pickerallowsEditing]) {

        //获取用户编辑之后的图像

            theImage = [infoobjectForKey:UIImagePickerControllerEditedImage];


        }else{

            //获取原始的照片

            theImage = [infoobjectForKey:UIImagePickerControllerOriginalImage];

        }

        //保存图片到相册中

        UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);


    }else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){

        //判断获取类型:视频,并且是刚拍摄的视频

        //获取视频文件的URL

        NSURL * mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

        //创建ALAssetsLibrary对象并将视频保存到媒体库

        ALAssetsLibrary* assetsLibrary = [[ALAssetsLibraryalloc]init];

        //将视频保存到相册中

        [assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:mediaURLcompletionBlock:^(NSURL*assetURL,NSError*error) {

            //如果没有错误显示保存成功

            if(!error) {

                NSLog(@"保存成功");

            }else{

                NSLog(@"保存视频出现错误");

            }


        }];

    }

        // 隐藏UIImagePickerController

        [pickerdismissViewControllerAnimated:YES completion:nil];

}

#pragma mark - 当用户取消时,调用该方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{

    NSLog(@"用户取消拍摄");

    //隐藏UIimagePickerController

    [pickerdismissViewControllerAnimated:YES completion:nil];

}

你可能感兴趣的:(系统相册,相机,拍视频)