自定义照相机界面之cameraOverlayView和UIImagePickerControllerEditedImage

默认情况下 ,调用照相机的话,照相机下方有个白色的圆形按钮,这个按钮是可以自定义的,也可以隐藏,
    imagePicker.showsCameraControls=NO;//禁用摄像头控件
如果想自定义界面的话,可以调用摄像头控件的隐藏,并覆盖图层,或者说叠加层
 
  
            //将视图设置为摄像头的叠加层
        imagePicker.cameraOverlayView = overLayView;
overLayView是自定义的,和普通的view一样,不过需要有个拍照按钮,当然也可以加背景进去。图为自己加了个按钮进去。 自定义照相机界面之cameraOverlayView和UIImagePickerControllerEditedImage_第1张图片
 
  






- (IBAction)takePicture:(id)sender
{
    imagePicker = [[UIImagePickerController alloc] init];

    // If the device ahs a camera, take a picture, otherwise,
    // just pick from the photo library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        imagePicker.showsCameraControls=NO;//禁用摄像头控件
        
        //创建叠加层
        UIView *overLayView=[[UIView alloc]initWithFrame:self.view.bounds];
        
        
        UIImageView *crosshairs = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        
        crosshairs.frame = CGRectMake(self.view.center.x - 25,
                                            self.view.center.y - 25,
                                            50,
                                            50);
        crosshairs.image = [UIImage imageNamed:@"logo"];
        crosshairs.alpha = 0.5f;
        crosshairs.contentMode = UIViewContentModeCenter;
        
        [overLayView addSubview:crosshairs];
            //将视图设置为摄像头的叠加层
        imagePicker.cameraOverlayView = overLayView;
        
        //在叠加视图上自定义一个拍照按钮
        UIButton *takePhotoBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [takePhotoBtn setFrame:CGRectMake(74, 370, 178, 37)];
        [takePhotoBtn setTitle:@"选取图片" forState:UIControlStateNormal];
        [takePhotoBtn addTarget:self action:@selector(takePhoto:) forControlEvents:UIControlEventTouchUpInside];
        [overLayView addSubview:takePhotoBtn];
        
        
        
    } else {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }

    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    [self flashModeOn];
    [self cameraOriention];
    // Place image picker on the screen
    [self presentViewController:imagePicker animated:YES completion:NULL];
}

- (IBAction)backgroundTapped:(id)sender
{
    [self.view endEditing:YES];
}





- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *oldKey = self.item.itemKey;

    // Did the item already have an image?
    if (oldKey) {
        // Delete the old image
        [[BNRImageStore sharedStore] deleteImageForKey:oldKey];
    }

    UIImage *image;
    // Get picked image from info dictionary
    if (info[UIImagePickerControllerEditedImage]) {
        image =info[UIImagePickerControllerEditedImage];
    }
    else
    {
      image = info[UIImagePickerControllerOriginalImage];
    }
    
    


    // Store the image in the BNRImageStore for this key
    [[BNRImageStore sharedStore] setImage:image forKey:self.item.itemKey];

    // Put that image onto the screen in our image view
    self.imageView.image = image;
  
    // Take image picker off the screen -
    // you must call this dismiss method
    [self dismissViewControllerAnimated:YES completion:NULL];
}




//闪光灯
-(void)flashModeOn
{
    if (imagePicker.cameraFlashMode == UIImagePickerControllerCameraFlashModeAuto) {
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
    }
    else
    {
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    }
}



- (void)cameraOriention
{
    if (imagePicker.cameraDevice == UIImagePickerControllerCameraDeviceRear) {
        imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    }
    else
    {
        imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    }
}




- (void)takePhoto:(id)sender
{
    [imagePicker takePicture];
}








第二个需要注意的是如何使用编辑的照片,
1,需要设定可编辑:
    imagePicker.allowsEditing = YES;
2,需要选择编辑过的照片,(只能放大缩小)
 UIImage *image;
    // Get picked image from info dictionary
    if (info[UIImagePickerControllerEditedImage]) {
        image =info[UIImagePickerControllerEditedImage];
    }
    else
    {
      image = info[UIImagePickerControllerOriginalImage];
    }
如果直接使用了,
UIImagePickerControllerEditedImage,而没有做第一步的话,那么程序就会崩溃。
 
  
 
  

你可能感兴趣的:(ios)