美文网首页
相机、相册的使用

相机、相册的使用

作者: 古月思吉 | 来源:发表于2018-08-26 12:10 被阅读0次
  • 注:本文只展示相机、相册的基本用法
//遵循代理
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


//设置变量
UIImagePickerController *_imagePickerController;//调取系统相机、相册


#pragma mark - 相机、相册相关
//创建_imagePickerController
- (void) createImagePickerController {
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
        _imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        _imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie];//图片是:kUTTypeImage
        _imagePickerController.allowsEditing = YES;
    }
}
//从摄像头获取视频(或图片)
- (void)selectImageFromCamera
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

    //下面是video需要设置的属性
    _imagePickerController.videoMaximumDuration = 30;//录制视频时长,默认10s
    _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;//视频上传质量
    _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;//设置摄像头模式(拍照,录制视频)为录像模式

    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
//从相册获取视频(或图片)
- (void)selectImageFromAlbum
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
//获取资源成功代理方法(适用获取所有媒体资源,只需判断资源类型)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //如果是图片
        UIImage * image = info[UIImagePickerControllerEditedImage];
        //压缩图片
        NSData *fileData = UIImageJPEGRepresentation(image, 1.0);
        //上传图片
        //[self uploadImageWithData:fileData];
    }else{
        //如果是视频
        NSURL *url = info[UIImagePickerControllerMediaURL];
        NSData *videoData = [NSData dataWithContentsOfURL:url];
        //视频上传
        //[self uploadVideoWithData:videoData];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
//“取消”操作代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

参考文章:
https://www.jianshu.com/p/e70a184d1f32

相关文章

  • 使用相册、相机

    首先需要在info.plist文件中添加以下键值对: 剩下的废话不多说,直接上代码:

  • 相机、相册的使用

    注:本文只展示相机、相册的基本用法 参考文章:https://www.jianshu.com/p/e70a184d...

  • 图片多选的实现

    图片多选的实现 分为两部分1 调用相机拍照添加图片2 打开相册添加图片 1 使用相机添加图片 打开相册添加图片使用...

  • iOS10更新的坑

    相册,相机,麦克风等使用会崩溃。iOS10的新特性,以后使用相机,相册,麦克风,通讯录都要在Info.plist文...

  • oc 扫码

    使用相机扫码 扫描相册 1. 使用相机扫码 相机授权[https://www.jianshu.com/p/f3cf...

  • iOS - 拍照功能、选择照片功能和选择视频,长按删除、预览等

    一.使用 UIPickerViewController 自定义相机和相册功能 ** 第一.调用相机方法 (系统版)...

  • iOS调用系统相册、相机 显示中文标题

    最近做头像上传功能需要使用系统相册、相机,在调用系统相册、相机发现是英文的系统相簿界面后标题显示“photos”,...

  • Android 相机/相册 图片裁剪

    打开相机 打开相册 **使用 uCrop 裁剪 详情请参考 Android 头像选择(拍照、相册裁剪),含7.0的...

  • 13.4 camera

    简介 android中相机使用有两种方式,一是调用系统相机(Intent),二是自定义相机 调用系统相机相册 自定...

  • 相册、相机

    添加依赖 1、清单文件里的权限 2、相机权限、相册权限 3、相机 4、相册 5、相机、相册返回的数据 6、上传 7...

网友评论

      本文标题:相机、相册的使用

      本文链接:https://www.haomeiwen.com/subject/zsfoiftx.html