IOS 获取系统照片和拍照

1.首先设置照片获取与选择以及NAV的代理

@interface MainViewController ()<CHPhotoImageChooseDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    CHPhotoImageChooseView *_photoView;
}

2.获取图片的两种方法如下:

-(void)chooseTakePhotoBtn{
    
    //资源类型为照相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //判断是否有相机
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        //资源类型为照相机
        picker.sourceType = sourceType;
        [_photoView dismissViews];
        
        //[self presentModalViewController:picker animated:YES];
        [self presentViewController:picker animated: YES completion:nil] ;
        
        [picker release];
    }else {
        NSLog(@"该设备无摄像头");
    }
}

- (void)chooseLocalPhotoBtn{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //资源类型为图片库
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [_photoView dismissViews];
    
    // [self presentModalViewController:picker animated:YES];
    [self presentViewController:picker animated: YES completion:nil] ;
    
}


3.从相册或者拍照结束后,通过下面的方法回调获取图片

#pragma Delegate method UIImagePickerControllerDelegate
//图像选取器的委托方法,选完图片后回调该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    
    //当图片不为空时显示图片并保存图片
    if (image != nil) {
        //图片显示在界面上
        [changeImage setBackgroundImage:image forState:UIControlStateNormal];
        
        //以下是保存文件到沙盒路径下
        //把图片转成NSData类型的数据来保存文件
        NSData *data;
        //判断图片是不是png格式的文件
        if (UIImagePNGRepresentation(image)) {
            //返回为png图像。
            data = UIImagePNGRepresentation(image);
        }else {
            //返回为JPEG图像。
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        //保存
        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];
        
    }
    //关闭相册界面
    [picker dismissModalViewControllerAnimated:YES];
}


4. 自定义的照片选择UIview显示与消失(含屏幕罩)

- (void)dismissViews
{
    CGRect tempFrame = _CHViews.frame;
    tempFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
    
    [UIView animateWithDuration:0.3f
                     animations:^{
                         _CHViews.frame = tempFrame;
                     }
                     completion:^(BOOL finished) {
                         [_CHViews removeFromSuperview];
                         [_backgroundView removeFromSuperview];
                     }];
}

- (void)show{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window setFrame:[[UIScreen mainScreen] bounds]];
    [window makeKeyWindow];
    //[window addSubview:_CHViews];
    
    if (window.frame.size.height > _CHViews.frame.size.height) {
        if (!_backgroundView) {
            
            _backgroundView = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];
            
            _backgroundView.backgroundColor = [UIColor lightGrayColor];
            
            _backgroundView.alpha = 0.4;
            
            [_backgroundView addTarget:self action:@selector(dismissViews) forControlEvents:UIControlEventTouchDown];
        }
        [window addSubview:_backgroundView];
        
        CGRect tempFrame = _CHViews.frame;
        CGFloat destOriginY = window.frame.size.height - _CHViews.frame.size.height;
        
        if (tempFrame.origin.y == destOriginY)
        { //此处为了动画是从下往上弹出的
            tempFrame.origin.y = window.frame.size.height;
            _CHViews.frame = tempFrame;
        }
        tempFrame.origin.y = destOriginY;
        
        [window addSubview:_CHViews];
        
        
        [UIView animateWithDuration:0.3f
                         animations:^{
                             _CHViews.frame = tempFrame;
                         }
                         completion:nil];
    }
}


over

demo下载地址

你可能感兴趣的:(IOS 获取系统照片和拍照)