ios使用系统UIImagePickerController 相

2018-12-10  本文已影响0人  yyggzc521
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- (void)selectImageSource {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;//编辑模式  但是编辑框是正方形的
// 使用前置还是后置摄像头
        imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceRear;

  #// 设置可用的媒体类型、默认只包含kUTTypeImage
imagePickerVC.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"从相机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
//设置照片来源

            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    [actionSheet addAction:cameraAction];
    [actionSheet addAction:photoAction];
    [actionSheet addAction:cancelAction];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
}


#pragma mark - =======UIImagePickerControllerDelegate=========

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    [self calulateImageFileSize:image];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"]) {
        switch (self.selectImageType) {
            case ZCSelectImageTypeFace: {
                [self.IdentityCardCell showFaceImage:image];
            }
                break;
            case ZCSelectImageTypeBack: {
                [self.IdentityCardCell showBackImage:image];
            }
                break;
            case ZCSelectImageTypeBusiness: {
                [self.businessLicenseCell showBusinessLicenseImage:image];
            }
                break;
        }
        
    }
}

字典info中key的含义
UIImagePickerControllerCropRect // 编辑裁剪区域
UIImagePickerControllerEditedImage // 编辑后的UIImage
UIImagePickerControllerMediaType // 返回媒体的媒体类型
UIImagePickerControllerOriginalImage  // 原始的UIImage
UIImagePickerControllerReferenceURL // 图片地址

// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:nil];
}

计算UIImage图片大小

- (void)calulateImageFileSize:(UIImage *)image {
    
    NSData *data = UIImagePNGRepresentation(image);
    if (!data) {
        data = UIImageJPEGRepresentation(image, 0.5);//需要改成0.5才接近原图片大小,原因请看下文
    }
    double dataLength = [data length] * 1.0;
    NSArray *typeArray = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB",@"ZB",@"YB"];
    NSInteger index = 0;
    while (dataLength > 1024) {
        dataLength /= 1024.0;
        index ++;
    }
    NSLog(@"image = %.3f %@",dataLength,typeArray[index]);
}

参考:
https://blog.csdn.net/shihuboke/article/details/77150958
https://blog.csdn.net/w582324909/article/details/79625613
https://www.jianshu.com/p/48ffc684c881
https://www.jianshu.com/p/ea475f608526

上一篇 下一篇

猜你喜欢

热点阅读