美文网首页iOS OC
iOS 获取相册图片(拍照或者从手机相册选择)

iOS 获取相册图片(拍照或者从手机相册选择)

作者: 流年小书 | 来源:发表于2018-06-28 14:16 被阅读7次

    选择相册照片或者拍照

      __weak typeof(self) weakSelf = self;
        TOActionSheet *actionSheet = [[TOActionSheet alloc] init];
        actionSheet.contentstyle = TOActionSheetContentStyleDefault;
        actionSheet.style = TOActionSheetStylekinema;
        [actionSheet addButtonWithTitle:@"拍照" icon:nil  tappedBlock:^{
            __strong typeof(weakSelf) sself = weakSelf;
            [sself makePhoto];
        }];
        [actionSheet addButtonWithTitle:@"从手机相册选择" icon:nil  tappedBlock:^{
            __strong typeof(weakSelf) sself = weakSelf;
            [sself choosePicture];
        }];
        [actionSheet showFromView:self.certificateImgeView inView:weakSelf.view];
    

    拍照

     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }
    

    从相册获取照片

      if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }
    

    代理

    //获取到图片
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [self dismissViewControllerAnimated:YES completion:^{
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            [self compressOriginalImage:image toMaxDataSizeKBytes:200];
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            if (image) {
                [defaults setObject:[self compressOriginalImage:image toMaxDataSizeKBytes:200]  forKey:@"certificateImg"];
            }
            [self.tableView reloadData];
        }];
    }
    
    //取消获取照片
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    相关文章

      网友评论

        本文标题:iOS 获取相册图片(拍照或者从手机相册选择)

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