0

我正在尝试制作我的第一个 iOS 应用程序,但在获取刚刚拍摄的照片时遇到了一些问题。

这是我的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //add imagePicker
    _imgPicker = [[UIImagePickerController alloc] init];
    _imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    _imgPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    _imgPicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    _imgPicker.showsCameraControls = NO;
    _imgPicker.navigationBarHidden = YES;
    _imgPicker.toolbarHidden = YES;
    _imgPicker.wantsFullScreenLayout = NO;

    //add overlay on imagePicker
    _imageSilhouette.image = [UIImage imageNamed:_pose.image];
    _imgPicker.cameraOverlayView = _imageSilhouette;
    [_vImagePreview addSubview:_imgPicker.view];
}

当我点击一个按钮时,我有一个计时器,10秒后,我拍照:

- (void)decreaseTimerCount
{
    _lblTimer.text = [NSString stringWithFormat:@"%d", --_timerCount];
    if(_timerCount==0)
    {
        [self takePicture];
    }
}
-(void) takePicture{
    [_timer invalidate];
    [_imgPicker takePicture];
}

正如我在 Apple 文档中看到的,我添加了委托:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * img = [info objectForKey:UIImagePickerControllerOriginalImage];
    _imageSilhouette.image = img;
    _imgPicker.cameraOverlayView = _imageSilhouette;
    //[picker dismissModalViewControllerAnimated:YES];
}

但我真的不明白我是否必须在某处声明 imagePickerController ..

我也不明白 img 是否包含我刚拍的照片?

我能帮忙吗?

4

1 回答 1

2

您没有设置选择器的代表

在您设置图像选择器的代码中,您需要添加以下内容:

_imgPicker.delegate = self;

此外,在.h您制作选择器的文件的文件中,您需要在<>'s之间添加代码

@interface SomeClass:NSObject <UIImagePickerControllerDelegate>

除了在 .h 文件中添加协议之外,您还可以选择在 .m 文件中添加(更好的)选项。这样做可以避免告诉世界您的班级使用了图像选择器:

@interface SomeClass () <UIImagePickerControllerDelegate>
@end

@implementation在相应行之前添加它。

于 2013-04-09T15:21:49.550 回答