将视图添加到UIViewController上

时间:2015-04-07 03:58:40

标签: xcode camera uiimagepickercontroller

我制作了一个UIImagePicker

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

我想添加一个半圈,所以我这样做,

CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextBeginPath(gc);
    CGContextAddArc(gc, 100, 100, 50, -M_PI_2, M_PI_2, 1);
    CGContextClosePath(gc); // could be omitted
    CGContextSetFillColorWithColor(gc, [UIColor cyanColor].CGColor);
    CGContextFillPath(gc);
    UIView *someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
    [someView.layer renderInContext:gc];
    [picker setCameraOverlayView:someView];

但是当我像这样展示选择器时

[self presentViewController:picker animated:YES completion:NULL];

我没看到半圆?为什么会这样?

谢谢

1 个答案:

答案 0 :(得分:1)

嘿,你没有正确的方式。

执行以下步骤可以帮助您......

见下面的答案.. https://stackoverflow.com/questions/5552210/uitableviewcells-invalid-context-trying-to-draw

<强> CircleView.h

#import <UIKit/UIKit.h>

@interface CircleView : UIView

@end

<强> CircleView.m

#import "CircleView.h"

@implementation CircleView

- (void)drawRect:(CGRect)rect 
  {
    CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextBeginPath(gc);
    CGContextAddArc(gc, 100, 100, 50, -M_PI_2, M_PI_2, 1);
    CGContextClosePath(gc); // could be omitted
    CGContextSetFillColorWithColor(gc, [UIColor cyanColor].CGColor);
    CGContextFillPath(gc);
 }
 @end

在您的imagepicker中添加半圈的意图在这里

 UIImagePickerController *controller = [[UIImagePickerController   alloc]init];
 controller.delegate=self;
 controller.sourceType = UIImagePickerControllerSourceTypeCamera;
 [self presentViewController:controller animated:YES completion:^{
            CircleView *someView = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
            [someView setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
            [controller setCameraOverlayView:someView];
        }];