FLTImagePickerPlugin.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2013 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "FLTImagePickerPlugin.h"
  5. #import <AVFoundation/AVFoundation.h>
  6. #import <MobileCoreServices/MobileCoreServices.h>
  7. #import <Photos/Photos.h>
  8. #import <UIKit/UIKit.h>
  9. #import "FLTImagePickerImageUtil.h"
  10. #import "FLTImagePickerMetaDataUtil.h"
  11. #import "FLTImagePickerPhotoAssetUtil.h"
  12. @interface FLTImagePickerPlugin () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
  13. @property(copy, nonatomic) FlutterResult result;
  14. @end
  15. static const int SOURCE_CAMERA = 0;
  16. static const int SOURCE_GALLERY = 1;
  17. @implementation FLTImagePickerPlugin {
  18. NSDictionary *_arguments;
  19. UIImagePickerController *_imagePickerController;
  20. UIImagePickerControllerCameraDevice _device;
  21. }
  22. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
  23. FlutterMethodChannel *channel =
  24. [FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/image_picker"
  25. binaryMessenger:[registrar messenger]];
  26. FLTImagePickerPlugin *instance = [FLTImagePickerPlugin new];
  27. [registrar addMethodCallDelegate:instance channel:channel];
  28. }
  29. - (UIImagePickerController *)getImagePickerController {
  30. return _imagePickerController;
  31. }
  32. - (UIViewController *)viewControllerWithWindow:(UIWindow *)window {
  33. UIWindow *windowToUse = window;
  34. if (windowToUse == nil) {
  35. for (UIWindow *window in [UIApplication sharedApplication].windows) {
  36. if (window.isKeyWindow) {
  37. windowToUse = window;
  38. break;
  39. }
  40. }
  41. }
  42. UIViewController *topController = windowToUse.rootViewController;
  43. while (topController.presentedViewController) {
  44. topController = topController.presentedViewController;
  45. }
  46. return topController;
  47. }
  48. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  49. if (self.result) {
  50. self.result([FlutterError errorWithCode:@"multiple_request"
  51. message:@"Cancelled by a second request"
  52. details:nil]);
  53. self.result = nil;
  54. }
  55. if ([@"pickImage" isEqualToString:call.method]) {
  56. _imagePickerController = [[UIImagePickerController alloc] init];
  57. _imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
  58. _imagePickerController.delegate = self;
  59. _imagePickerController.mediaTypes = @[ (NSString *)kUTTypeImage ];
  60. self.result = result;
  61. _arguments = call.arguments;
  62. int imageSource = [[_arguments objectForKey:@"source"] intValue];
  63. switch (imageSource) {
  64. case SOURCE_CAMERA: {
  65. NSInteger cameraDevice = [[_arguments objectForKey:@"cameraDevice"] intValue];
  66. _device = (cameraDevice == 1) ? UIImagePickerControllerCameraDeviceFront
  67. : UIImagePickerControllerCameraDeviceRear;
  68. [self checkCameraAuthorization];
  69. break;
  70. }
  71. case SOURCE_GALLERY:
  72. [self checkPhotoAuthorization];
  73. break;
  74. default:
  75. result([FlutterError errorWithCode:@"invalid_source"
  76. message:@"Invalid image source."
  77. details:nil]);
  78. break;
  79. }
  80. } else if ([@"pickVideo" isEqualToString:call.method]) {
  81. _imagePickerController = [[UIImagePickerController alloc] init];
  82. _imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
  83. _imagePickerController.delegate = self;
  84. _imagePickerController.mediaTypes = @[
  85. (NSString *)kUTTypeMovie, (NSString *)kUTTypeAVIMovie, (NSString *)kUTTypeVideo,
  86. (NSString *)kUTTypeMPEG4
  87. ];
  88. _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
  89. self.result = result;
  90. _arguments = call.arguments;
  91. int imageSource = [[_arguments objectForKey:@"source"] intValue];
  92. if ([[_arguments objectForKey:@"maxDuration"] isKindOfClass:[NSNumber class]]) {
  93. NSTimeInterval max = [[_arguments objectForKey:@"maxDuration"] doubleValue];
  94. _imagePickerController.videoMaximumDuration = max;
  95. }
  96. switch (imageSource) {
  97. case SOURCE_CAMERA:
  98. [self checkCameraAuthorization];
  99. break;
  100. case SOURCE_GALLERY:
  101. [self checkPhotoAuthorization];
  102. break;
  103. default:
  104. result([FlutterError errorWithCode:@"invalid_source"
  105. message:@"Invalid video source."
  106. details:nil]);
  107. break;
  108. }
  109. } else {
  110. result(FlutterMethodNotImplemented);
  111. }
  112. }
  113. - (void)showCamera {
  114. @synchronized(self) {
  115. if (_imagePickerController.beingPresented) {
  116. return;
  117. }
  118. }
  119. // Camera is not available on simulators
  120. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] &&
  121. [UIImagePickerController isCameraDeviceAvailable:_device]) {
  122. _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  123. _imagePickerController.cameraDevice = _device;
  124. [[self viewControllerWithWindow:nil] presentViewController:_imagePickerController
  125. animated:YES
  126. completion:nil];
  127. } else {
  128. [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil)
  129. message:NSLocalizedString(@"Camera not available.", nil)
  130. delegate:nil
  131. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  132. otherButtonTitles:nil] show];
  133. self.result(nil);
  134. self.result = nil;
  135. _arguments = nil;
  136. }
  137. }
  138. - (void)checkCameraAuthorization {
  139. AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  140. switch (status) {
  141. case AVAuthorizationStatusAuthorized:
  142. [self showCamera];
  143. break;
  144. case AVAuthorizationStatusNotDetermined: {
  145. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
  146. completionHandler:^(BOOL granted) {
  147. if (granted) {
  148. dispatch_async(dispatch_get_main_queue(), ^{
  149. if (granted) {
  150. [self showCamera];
  151. }
  152. });
  153. } else {
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. [self errorNoCameraAccess:AVAuthorizationStatusDenied];
  156. });
  157. }
  158. }];
  159. }; break;
  160. case AVAuthorizationStatusDenied:
  161. case AVAuthorizationStatusRestricted:
  162. default:
  163. [self errorNoCameraAccess:status];
  164. break;
  165. }
  166. }
  167. - (void)checkPhotoAuthorization {
  168. PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
  169. switch (status) {
  170. case PHAuthorizationStatusNotDetermined: {
  171. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  172. if (status == PHAuthorizationStatusAuthorized) {
  173. dispatch_async(dispatch_get_main_queue(), ^{
  174. [self showPhotoLibrary];
  175. });
  176. } else {
  177. [self errorNoPhotoAccess:status];
  178. }
  179. }];
  180. break;
  181. }
  182. case PHAuthorizationStatusAuthorized:
  183. [self showPhotoLibrary];
  184. break;
  185. case PHAuthorizationStatusDenied:
  186. case PHAuthorizationStatusRestricted:
  187. default:
  188. [self errorNoPhotoAccess:status];
  189. break;
  190. }
  191. }
  192. - (void)errorNoCameraAccess:(AVAuthorizationStatus)status {
  193. switch (status) {
  194. case AVAuthorizationStatusRestricted:
  195. self.result([FlutterError errorWithCode:@"camera_access_restricted"
  196. message:@"The user is not allowed to use the camera."
  197. details:nil]);
  198. break;
  199. case AVAuthorizationStatusDenied:
  200. default:
  201. self.result([FlutterError errorWithCode:@"camera_access_denied"
  202. message:@"The user did not allow camera access."
  203. details:nil]);
  204. break;
  205. }
  206. }
  207. - (void)errorNoPhotoAccess:(PHAuthorizationStatus)status {
  208. switch (status) {
  209. case PHAuthorizationStatusRestricted:
  210. self.result([FlutterError errorWithCode:@"photo_access_restricted"
  211. message:@"The user is not allowed to use the photo."
  212. details:nil]);
  213. break;
  214. case PHAuthorizationStatusDenied:
  215. default:
  216. self.result([FlutterError errorWithCode:@"photo_access_denied"
  217. message:@"The user did not allow photo access."
  218. details:nil]);
  219. break;
  220. }
  221. }
  222. - (void)showPhotoLibrary {
  223. // No need to check if SourceType is available. It always is.
  224. _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  225. [[self viewControllerWithWindow:nil] presentViewController:_imagePickerController
  226. animated:YES
  227. completion:nil];
  228. }
  229. - (void)imagePickerController:(UIImagePickerController *)picker
  230. didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
  231. NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
  232. [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
  233. // The method dismissViewControllerAnimated does not immediately prevent
  234. // further didFinishPickingMediaWithInfo invocations. A nil check is necessary
  235. // to prevent below code to be unwantly executed multiple times and cause a
  236. // crash.
  237. if (!self.result) {
  238. return;
  239. }
  240. if (videoURL != nil) {
  241. if (@available(iOS 13.0, *)) {
  242. NSString *fileName = [videoURL lastPathComponent];
  243. NSURL *destination =
  244. [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:fileName]];
  245. if ([[NSFileManager defaultManager] isReadableFileAtPath:[videoURL path]]) {
  246. NSError *error;
  247. if (![[videoURL path] isEqualToString:[destination path]]) {
  248. [[NSFileManager defaultManager] copyItemAtURL:videoURL toURL:destination error:&error];
  249. if (error) {
  250. self.result([FlutterError errorWithCode:@"flutter_image_picker_copy_video_error"
  251. message:@"Could not cache the video file."
  252. details:nil]);
  253. self.result = nil;
  254. return;
  255. }
  256. }
  257. videoURL = destination;
  258. }
  259. }
  260. self.result(videoURL.path);
  261. self.result = nil;
  262. _arguments = nil;
  263. } else {
  264. UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  265. if (image == nil) {
  266. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  267. }
  268. NSNumber *maxWidth = [_arguments objectForKey:@"maxWidth"];
  269. NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
  270. NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
  271. if (![imageQuality isKindOfClass:[NSNumber class]]) {
  272. imageQuality = @1;
  273. } else if (imageQuality.intValue < 0 || imageQuality.intValue > 100) {
  274. imageQuality = [NSNumber numberWithInt:1];
  275. } else {
  276. imageQuality = @([imageQuality floatValue] / 100);
  277. }
  278. if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
  279. image = [FLTImagePickerImageUtil scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
  280. }
  281. PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
  282. if (!originalAsset) {
  283. // Image picked without an original asset (e.g. User took a photo directly)
  284. [self saveImageWithPickerInfo:info image:image imageQuality:imageQuality];
  285. } else {
  286. __weak typeof(self) weakSelf = self;
  287. [[PHImageManager defaultManager]
  288. requestImageDataForAsset:originalAsset
  289. options:nil
  290. resultHandler:^(NSData *_Nullable imageData, NSString *_Nullable dataUTI,
  291. UIImageOrientation orientation, NSDictionary *_Nullable info) {
  292. // maxWidth and maxHeight are used only for GIF images.
  293. [weakSelf saveImageWithOriginalImageData:imageData
  294. image:image
  295. maxWidth:maxWidth
  296. maxHeight:maxHeight
  297. imageQuality:imageQuality];
  298. }];
  299. }
  300. }
  301. }
  302. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  303. [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
  304. if (!self.result) {
  305. return;
  306. }
  307. self.result(nil);
  308. self.result = nil;
  309. _arguments = nil;
  310. }
  311. - (void)saveImageWithOriginalImageData:(NSData *)originalImageData
  312. image:(UIImage *)image
  313. maxWidth:(NSNumber *)maxWidth
  314. maxHeight:(NSNumber *)maxHeight
  315. imageQuality:(NSNumber *)imageQuality {
  316. NSString *savedPath =
  317. [FLTImagePickerPhotoAssetUtil saveImageWithOriginalImageData:originalImageData
  318. image:image
  319. maxWidth:maxWidth
  320. maxHeight:maxHeight
  321. imageQuality:imageQuality];
  322. [self handleSavedPath:savedPath];
  323. }
  324. - (void)saveImageWithPickerInfo:(NSDictionary *)info
  325. image:(UIImage *)image
  326. imageQuality:(NSNumber *)imageQuality {
  327. NSString *savedPath = [FLTImagePickerPhotoAssetUtil saveImageWithPickerInfo:info
  328. image:image
  329. imageQuality:imageQuality];
  330. [self handleSavedPath:savedPath];
  331. }
  332. - (void)handleSavedPath:(NSString *)path {
  333. if (!self.result) {
  334. return;
  335. }
  336. if (path) {
  337. self.result(path);
  338. } else {
  339. self.result([FlutterError errorWithCode:@"create_error"
  340. message:@"Temporary file could not be created"
  341. details:nil]);
  342. }
  343. self.result = nil;
  344. _arguments = nil;
  345. }
  346. @end