0

当我运行我的代码并使用下面的代码启动相机时,我会得到我的自定义叠加视图以及默认的相机按钮。唯一的问题是,现在,我的 UIImagePicker 曾经所在的位置出现了一个空白黑框。我只是尝试使用默认的 UIImagePicker 控件和按钮创建一个相机,然后将我自己的 IBOutlet UILabel 添加到默认的 imagePickerController 之上,以便我的标签与所有默认的相机控件一起显示。不过,我在完成这件事时遇到了可怕的事情。干杯!

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.image == nil &&  [self.videoFilePath length] == 0) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = NO;
        self.imagePicker.videoMaximumDuration = 10;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

        [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
        self.overlayView.frame = self.imagePicker.cameraOverlayView.frame;
        self.imagePicker.cameraOverlayView = self.overlayView;
        self.overlayView = nil;
    }
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}
4

2 回答 2

0

用这个修复它。我对 CGPoints & CGRect 做了一些研究并提出了这个解决方案。不确定它是否是最优雅的解决方案,但它有效并且似乎很有意义。- (void)viewDidLoad { [超级 viewDidLoad];

    if (self.image == nil &&  [self.videoFilePath length] == 0) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = YES;
        self.imagePicker.videoMaximumDuration = 10;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
            }
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    self.overlayView.frame = CGRectMake(160,0,0,0);
    self.imagePicker.cameraOverlayView = self.overlayView;

}
于 2013-09-03T09:12:49.817 回答
0

尝试使用我的代码。它工作得很好。可能是你达到你想要的。我创建了一个控制器,并使用它的视图来设置cameraOverlayView属性。我没有使用 nib 文件,我以编程方式创建了所有内容。

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.tabBarItem.image = [UIImage imageNamed:@"86-camera"];

self.cameraOverlayViewController = [[CameraOverlayViewController alloc] init];
self.cameraOverlayViewController.picker = picker;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.showsCameraControls = NO;
    picker.toolbarHidden = YES;
    picker.cameraOverlayView = self.cameraOverlayViewController.view;
    picker.delegate = self.cameraOverlayViewController;

}
else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}

这是自定义代码CameraOverlayViewController:接口文件:

#import <UIKit/UIKit.h>

@interface CameraOverlayViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController *picker;

@end

实现文件:

#import "CameraOverlayViewController.h"
#import "BButton.h"
#import "AnalyseViewController.h"
#import "Tree.h"

@interface CameraOverlayViewController ()
{
}
@end

@implementation CameraOverlayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.view.backgroundColor = [UIColor clearColor];
        self.view.opaque = NO;

    }
    return self;
}


- (void)loadView
{
    [super loadView];

    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bottom - 150, 320, 150)];
    bottomView.backgroundColor = [UIColor blackColor];
    BButton *button = [[BButton alloc] initWithFrame:CGRectMake(50, 25, 220, 50) type:BButtonTypePrimary];
    [button setTitle:@"Сфотографировать" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:button];
    [self.view addSubview:bottomView];

    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 70)];
    topView.backgroundColor = [UIColor clearColor];
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 310, 60)];
    infoLabel.backgroundColor = [UIColor clearColor];
    infoLabel.font = [UIFont fontWithName:@"Georgia" size:18];
    infoLabel.textColor = [UIColor whiteColor];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.numberOfLines = 0;
    infoLabel.text = @"Поместите лист дерева внутри рамки, чтобы\nсфотографировать его.";
    [topView addSubview:infoLabel];

    [self.view addSubview:topView];

    UIView *bordersView = [[UIView alloc] initWithFrame:CGRectMake(10, topView.bottom + 10, 300, 300)];
    bordersView.layer.cornerRadius = 10;
    bordersView.layer.borderColor = [UIColor whiteColor].CGColor;
    bordersView.layer.borderWidth = 2;
    bordersView.backgroundColor = [UIColor clearColor];

    [self.view addSubview:bordersView];
}


#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

    AnalyseViewController *analyseViewController = [[AnalyseViewController alloc] initWithNibName:nil bundle:nil];
    analyseViewController.baseImage = image;
    analyseViewController.treesArray = [Tree retrieveAllTrees];

    UINavigationController *analyseNavController = [[UINavigationController alloc] initWithRootViewController:analyseViewController];

    [AppDel.tabBarController presentViewController:analyseNavController animated:YES completion:nil];
}


#pragma mark - Other methods

- (void)buttonClicked:(id)sender
{
    [self.picker takePicture];
}

@end

自己删除不需要的类,如 BButton。

于 2013-09-03T07:26:41.697 回答