如何从Method中启动ViewController?

时间:2014-11-29 11:10:01

标签: ios objective-c iphone cordova cordova-plugins

我想在iOS Cordova插件方法中使用TGCameraViewController

以下是该方法的实现:

#import "BCamera.h"
#import "TGCameraViewController.h"

@implementation BCamera

// Cordova command method
-(void) openCamera:(CDVInvokedUrlCommand *)command {

// take a photo

}

从TGCameraViewController的文档我必须使用以下内容拍照:

#import "TGCameraViewController.h"

@interface TGViewController : UIViewController <TGCameraDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *photoView;

- (IBAction)takePhotoTapped;

@end



@implementation TGViewController

- (IBAction)takePhotoTapped
{
    TGCameraNavigationController *navigationController =
    [TGCameraNavigationController newWithCameraDelegate:self];

    [self presentViewController:navigationController animated:YES completion:nil];
}

#pragma mark - TGCameraDelegate optional

- (void)cameraWillTakePhoto
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)cameraDidSavePhotoAtPath:(NSURL *)assetURL
{
    // When this method is implemented, an image will be saved on the user's device
    NSLog(@"%s album path: %@", __PRETTY_FUNCTION__, assetURL);
}

- (void)cameraDidSavePhotoWithError:(NSError *)error
{
    NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error);
}

#pragma mark - TGCameraDelegate required

- (void)cameraDidTakePhoto:(UIImage *)image
{
    _photoView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)cameraDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

如何使用此示例从openCamera方法中打开相机视图?

1 个答案:

答案 0 :(得分:1)

您应该在应用程序的rootViewController上显示TGCameraNavigationController,例如

@interface BCamera() <TGCameraDelegate>

@end

@implementation BCamera

- (void)openCamera:(CDVInvokedUrlCommand *) command
{    
    TGCameraNavigationController *navigationController = [TGCameraNavigationController newWithCameraDelegate:self];    
    UIViewController *rootVC = [[UIApplication sharedApplication] delegate] window] rootViewController];
    [rootVC presentViewController:navigationController animated:YES completion:nil];
}

此外,您必须实施TGCameraDelegate协议方法。

- (void)cameraDidTakePhoto:(UIImage *)image
{
    //do something with image

    [self dismissCameraVC];
}

- (void)cameraDidCancel
{
    [self dismissCameraVC];
} 
- (void)dismissCameraVC
{
    UIViewController *rootVC = [[UIApplication sharedApplication] delegate] window] rootViewController];    
    [rootVC dismissViewControllerAnimated:YES completion:nil];
}