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

来源:互联网 发布:淘宝自助服务在哪 编辑:程序博客网 时间:2024/05/22 13:10
默认情况下 ,调用照相机的话,照相机下方有个白色的圆形按钮,这个按钮是可以自定义的,也可以隐藏,
<pre name="code" class="objc">    imagePicker.showsCameraControls=NO;//禁用摄像头控件
如果想自定义界面的话,可以调用摄像头控件的隐藏,并覆盖图层,或者说叠加层
<pre name="code" class="objc"><span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px;">            //将视图设置为摄像头的叠加层</span>        imagePicker.cameraOverlayView = overLayView;
overLayView是自定义的,和普通的view一样,不过需要有个拍照按钮,当然也可以加背景进去。图为自己加了个按钮进去。
- (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];
<span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;">            //将视图设置为摄像头的叠加层</span>        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 *)pickerdidFinishPickingMediaWithInfo:(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,需要设定可编辑:<pre name="code" class="objc">    imagePicker.allowsEditing = YES;
2,需要选择编辑过的照片,(只能放大缩小)
<pre name="code" class="objc"> UIImage *image;    // Get picked image from info dictionary    if (info[UIImagePickerControllerEditedImage]) {        image =info[UIImagePickerControllerEditedImage];    }    else    {      image = info[UIImagePickerControllerOriginalImage];    }
如果直接使用了,
UIImagePickerControllerEditedImage,而没有做第一步的话,那么程序就会崩溃。

0 0
原创粉丝点击