YHNativeCamera.m
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// 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