调用系统的相册和摄像头问题

时间:2022-10-12 19:03:01

由于在一个程序中我们有很多地方需要上传图片,我们会通过摄像头或者是相册调取图片。如果每用到一个地方我们就重新写一次代码会造成文件的大量冗余,因此将他们进行封装到一个UIView中,只需要引入头文件,在UIViewController中使用两句代码就可以。由于代码比较多,粘贴到下面看着也会非常厌烦,因此将下载路径给出直接打开工程查看就好:http://code.cocoachina.com/view/134281

下面是UIView中的代码:

.h中的代码

#import <UIKit/UIKit.h>
typedef  void(^ImageBlock)(UIImage *image);

@interface SZImageView : UIView
@property(nonatomic,copy)ImageBlock imageBlock;
+(SZImageView *)shareImageView;
-(void)getFrame:(CGSize)size viewController:(id)viewController;
@end

.m中的代码

#define Widthscale(scale) ((([UIScreen mainScreen].bounds.size.width) * scale) / (375))
#define Heightscale(scale) ((([UIScreen mainScreen].bounds.size.height) * scale) / (667))
#import "SZImageView.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface SZImageView()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,strong)UINavigationController *navigation;
@property(nonatomic,strong) UIView *viewbg;
@property(nonatomic,strong)UIViewController *viewImageController;
@end
@implementation SZImageView
+(SZImageView *)shareImageView{
    static SZImageView *imageview = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imageview = [[[NSBundle mainBundle]loadNibNamed:@"SZImageView" owner:nil options:nil]lastObject];
    });
    return imageview;
}

-(void )getFrame:(CGSize)size viewController:(id)viewController{
    self.viewImageController = viewController;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.frame = [UIScreen mainScreen].bounds;
    _viewbg = [[UIView alloc]initWithFrame:window.frame];
    _viewbg.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    [window addSubview:_viewbg];
     [_viewbg addSubview:self];
   
    self.frame = CGRectMake(0, 0,Widthscale(size.width), Heightscale(size.height));
    self.center = window.center;
}

- (IBAction)openCamera:(UIButton*)sender {
    [self configImagePickerController:1];
}

- (IBAction)openPhoto:(UIButton*)sender {
    
     [self configImagePickerController:0];
}
- (IBAction)delect:(UIButton *)sender {
    [self delectAllView];
}
-(void)delectAllView{
        [self removeFromSuperview];
        [self.viewbg removeFromSuperview];
}
-(void)configImagePickerController:(NSInteger)number{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES; //可编辑
    
    switch (number) {
        case 0:
            //判断是否可以打开相册
            if ([self isPhotoLibrary]) {
                picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
            }else{
                NSLog(@"无法打开相册");
                return;
            }
            break;
        case 1:
            if ([self isCameravail]){
                //摄像头
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            }else{
                NSLog(@"没有摄像头");
                return;
            }
            break;
        default:
            break;
    }
    
    
    [self delectAllView];
    [self.viewImageController  presentViewController:picker animated:YES completion:nil];
   
}
//判断相机是否可用
-(BOOL)isCameravail{
    return   [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
-(BOOL)isPhotoLibrary{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSString *mdeiaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mdeiaType isEqualToString:(__bridge NSString*)kUTTypeImage]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            SEL saveImage = @selector(ImageWasSaveSuccessfully:didFinishSavingWithError:contextInfo:);
            UIImageWriteToSavedPhotosAlbum(image, self, saveImage, nil);
        }
            self.imageBlock(image);
    }else{
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)ImageWasSaveSuccessfully:(UIImage *)paraImage didFinishSavingWithError:(NSError *)error contextInfo:(NSDictionary<NSString *,id> *)paraInfo{
    if (error == nil) {
        NSLog(@"保存成功");
    }else{
        NSLog(@"%@",error);
    }
}


/* 下面的代码是一些相关的设置,如果想了解自己可以看一看
 */

//-(void)configImagePickerController{
//    
//    [self removeFromSuperview];
//    [self.viewbg removeFromSuperview];
//
//    UIImagePickerController *controller =[[UIImagePickerController alloc]init];
//    
//    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
//    NSString * requireMediaType = (__bridge NSString*)kUTTypeImage;
//    controller.mediaTypes = [[NSArray alloc]initWithObjects:requireMediaType, nil];
//    
//    controller.allowsEditing  = false;
//    controller.delegate =  self;
//    //打开闪光灯
//    controller.cameraFlashMode=  UIImagePickerControllerCameraFlashModeOn;
//    [self.viewImageController presentViewController:controller animated:YES completion:nil];
//    
//    
//}


//

////前置闪光灯是否可用
//-(BOOL)isCameraFlashavailFront{
//    return  [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceFront];
//}
////后置闪光灯是否可用
//-(BOOL)isCameraFlashavailRear{
//    return  [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
//}
//
////前摄像头是否可用
//-(BOOL)isCameraavailFront{
//    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
//}
////后置摄像头会否可用
//-(BOOL)isCameraavailRear{
//    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
//}
////判断是否支持拍照和录像
//-(BOOL)isCameraSupportMedia:(NSString *)paraMediaType
//{
//    NSArray *avaiablemedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//    for (NSString *item in avaiablemedia) {
//        if ([item isEqualToString:paraMediaType]) {
//            return true;
//        }
//    }
//    return false;
//}


@end


ViewController中的代码

#import "ViewController.h"
#import "SZImageView.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
   [SZImageView shareImageView].imageBlock  = ^(UIImage *image){
    self.imageView.image = image;
    
    };
}
- (IBAction)photoAlbum:(UIButton *)sender {
     [[SZImageView shareImageView]getFrame:CGSizeMake(200, 300) viewController:self];
}

- (IBAction)cameraClick:(UIButton *)sender {
    [[SZImageView shareImageView]getFrame:CGSizeMake(200, 300) viewController:self];
   }
@end



大家看看就好,只是一个粗体大框,只是提供一个思路而已,可以根据具体需求更改