美文网首页iOS开发
使用相册、相机

使用相册、相机

作者: 一蓑丨烟雨 | 来源:发表于2017-05-10 15:02 被阅读0次

首先需要在info.plist文件中添加以下键值对:

<key>NSCameraUsageDescription</key>
    <string>使用相机</string>

<key>NSPhotoLibraryUsageDescription</key>
    <string>使用相册</string>

剩下的废话不多说,直接上代码:

#pragma mark -- 点击添加用户的头像
-(void)addUserHeadImg:(UIButton *)btn{
    if (_user) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        __weak typeof(self) weakSelf = self;
        [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                [weakSelf showImagePickerVCWithType:UIImagePickerControllerSourceTypeCamera];
            }
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf showImagePickerVCWithType:UIImagePickerControllerSourceTypePhotoLibrary];
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

//拍照
-(void)showImagePickerVCWithType:(UIImagePickerControllerSourceType)type{
    //创建pickerVC
    UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
    //设置图片来源,可以使相机/相册
    pickerVC.sourceType = type;
    pickerVC.allowsEditing = YES;
    pickerVC.delegate = self;
    //弹出pickerVC
    [self presentViewController:pickerVC animated:YES completion:nil];
}

#pragma mark -- 获取用户选择的图片代理

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *img = info[@"UIImagePickerControllerEditedImage"];
    [_headImgV setImage:img forState:UIControlStateNormal];
    [_headImgV setImageEdgeInsets:UIEdgeInsetsZero];

    [self upDataHeadImageBox:img];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 把选择的图片存到本地
-(void)upDataHeadImageBox:(UIImage *)img{
    //更新沙盒
    NSMutableData *mutableData = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
    [archiver encodeObject:img forKey:kHeadKey];
    [archiver finishEncoding];
    [mutableData writeToFile:kFilePath atomically:YES];
}

相关文章

  • 使用相册、相机

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

  • 图片多选的实现

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

  • oc 扫码

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

  • 相机、相册的使用

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

  • iOS10更新的坑

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

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

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

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

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

  • Android 相机/相册 图片裁剪

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

  • 13.4 camera

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

  • H5 调用相机方法

    直接调用相机(测试安卓可以,iphone还是有相册) 调用相机 图片或者相册 调用相册

网友评论

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

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