相机控制器IOS的自定义形状

时间:2013-08-28 13:04:57

标签: ios uiimagepickercontroller

我正在尝试为相机视图提供圆形以拍摄图像。圆圈应该是200宽度和200高度,当它打开时,用户必须能够仍然与背景视图交互。我到目前为止所做的就是:

- (IBAction)useCameraPhoto:(id)sender
{

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker
                            animated:YES completion:nil];

    newMedia = YES;

}else{
    NSLog(@"Camera is not available");
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Important message" message:@"Unfortunately the camera is not available on your device." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert1 show];
}

} 

此代码提供了一个viewcontrller,用户可以从中拍摄,但它没有圆形。

我想过做类似的事情     imagePicker.allowsEditing.frame = CGRectMake(30,100,200,200); 但这仍然不会给它圆角。此外,它下面的视图,即使可见,也不会对触摸产生反应。有什么想法吗?

编辑:

我试过这个。它给出圆角但背景仍为黑色

imagePicker.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    imagePicker.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:imagePicker animated:YES completion:nil];

    imagePicker.view.layer.cornerRadius = 100; // this value vary as per your desire
    imagePicker.view.clipsToBounds = YES;
    imagePicker.view.frame = CGRectMake(40,40, 200, 200);

1 个答案:

答案 0 :(得分:1)

您可以使用AVFoundation直接添加相机支持,而不是进行模态弹出窗口。苹果开发者网站上有一个不错的iOS camera demo already available for free

然后,您可以将预览图像附加到UIViewController上的UIImageView,并对其图层执行相同的操作以获得所需的效果:

imageView.view.layer.cornerRadius = 100; // this value vary as per your desire
imageView.view.clipsToBounds = YES;
imageView.view.frame = CGRectMake(40,40, 200, 200);