IOS programming 4th Edition

11.camera
Gold Challenge
问题:利用UIImagePickerController 中cameraOverlayView属性。使其在取景视图的正中显示一个十字。
代码如下:
//添加照片

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

    //如果设备支持相机,就使用拍照模式
    //否则让用户从照片库中选择照片
    if ([UIImagePickerController
    isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = YES;
    //相机十字显示
    UIView *crosshair = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    crosshair.backgroundColor = [UIColor clearColor];
    //横线
    UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(0, 29.5, 60, 1)];
    line1.backgroundColor=[UIColor whiteColor];
    [crosshair addSubview:line1];
    //竖线
    UIView *line2=[[UIView alloc]initWithFrame:CGRectMake(29.5, 0, 1, 60)];
    line2.backgroundColor=[UIColor whiteColor];
    [crosshair addSubview:line2];

      crosshair.center = self.view.center;
      imagePicker.cameraOverlayView = crosshair;
    

    }else{
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;

    }
    //可编辑
    imagePicker.allowsEditing = YES;

    imagePicker.delegate = self;

    //以模态的形式显示UIImagePickerController对象
    [self presentViewController:imagePicker animated:YES completion:nil];
    }

你可能感兴趣的:(IOS programming 4th Edition)