YHNativeCamera.m 3.7 KB
//
//  YHNativeCamera.m
//  YohoExplorerDemo
//
//  Created by gaoqiang xu on 4/7/15.
//  Copyright (c) 2015 gaoqiang xu. All rights reserved.
//

#import "YHNativeCamera.h"
#import <UIKit/UIKit.h>
#import "UIImage+CropScaleOrientation.h"

NSString * const YHNative_Camera = @"Native_Camera";

static NSString *toBase64(NSData *data) {
    SEL s1 = NSSelectorFromString(@"cdv_base64EncodedString");
    SEL s2 = NSSelectorFromString(@"base64EncodedString");
    SEL realSel = [data respondsToSelector:s1] ? s1 : s2;
    NSString* (*func)(id, SEL) = (void *)[data methodForSelector:realSel];
    return func(data, realSel);
}

@interface YHNativeCamera ()
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) UIViewController *rootViewController;
@end

@implementation YHNativeCamera

- (NSString *)actionName
{
    return YHNative_Camera;
}

- (instancetype)init
{
    self = [super init];
    if (self) {

    }
    
    return self;
}

- (UIImagePickerControllerSourceType)sourceTypeFromString:(NSString *)str
{
    if ([str isEqualToString:@"CAMERA"]) {
        return UIImagePickerControllerSourceTypeCamera;
    } else if ([str isEqualToString:@"SAVEDPHOTOALBUM"]) {
        return UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    
    return UIImagePickerControllerSourceTypeCamera;
}

- (NSArray *)availableFunctions
{
    return @[ @"takePicture" ];
}

- (void)takePicture
{
    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]
        && ![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
        self.failureCallBack(@"No camera device available!");
        return;
    }
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = [self sourceTypeFromString:self.options[@"pictureSourceType"]];
    self.rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [self.rootViewController presentViewController:picker animated:YES completion:nil];
}

- (UIImage*)retrieveImage:(NSDictionary*)info
{
    // get the image
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    image = [image imageCorrectedForCaptureOrientation];
    
    NSDictionary *sizeInfo = self.options[@"targetSize"];
    if (sizeInfo) {
        CGFloat width = [sizeInfo[@"width"] doubleValue];
        CGFloat height = [sizeInfo[@"height"] doubleValue];
        
        if (width > 0 || height > 0) {
            UIImage *scaledImage = [image imageByScalingAndCroppingForSize:CGSizeMake(width, height)];
            if (scaledImage) {
                image = scaledImage;
            }
        }
    }
    
    return image;
}

- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info
{
    NSData *data = UIImageJPEGRepresentation(image, [self.options[@"quality"] floatValue] / 100.0f);
    
    if (!data) {
        data = UIImagePNGRepresentation(image);
    }
    
    return data;
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    UIImage *image = [self retrieveImage:info];
    NSData *data = [self processImage:image info:info];
    
    self.successCallBack(@{@"image": toBase64(data)}, NO, nil);
    [self.rootViewController dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self.rootViewController dismissViewControllerAnimated:YES completion:^{
        self.failureCallBack(@"Cancelled by user.");
    }];
}

@end