iOS 调用系统相机和选择相册照片
iOS 调用系统相机和选择相册照片
山里来的 发表于2年前
iOS 调用系统相机和选择相册照片
  • 发表于 2年前
  • 阅读 82
  • 收藏 0
  • 点赞 2
  • 评论 0

移动开发云端新模式探索实践 >>>   


// 在需要的地方调用下面方法  可是是button 也可以是imageView添加手势等
- (void)choosePhotos{

    UIAlertController *alert= [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:picker animated:YES completion:nil];
        
    }];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UIImagePickerController *camera = [[UIImagePickerController alloc] init];
        camera.delegate = self;
        camera.allowsEditing = YES;
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            camera.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else
        {
            NSLog( @"摄像头不可用");
            return;
        }
        camera.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        [self presentViewController:camera animated:YES completion:nil];
        
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    
    [alert addAction:sureAction];
    [alert addAction:cancelAction];
    [alert addAction:cameraAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}


//相册代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //参数UIImagePickerControllerEditedImage 是否裁剪
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    // image 就是我们选择的照片,可以展示,可以上传

    
        [picker dismissViewControllerAnimated:YES completion:nil];

}


相机界面不显示中文问题: 在info.plist 添加Localizations 选择Chinese(simplified) 即可 在info.plist 添加Localizations 选择Chinese(simplified) 即可

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 8
博文 82
码字总数 16936
×
山里来的
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: