UNPKG

nativescript-imagecropper-ios

Version:
139 lines (138 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var frame = require("tns-core-modules/ui/frame"); var image_source_1 = require("tns-core-modules/image-source"); var _options; var TOCropViewControllerDelegateImpl = (function (_super) { __extends(TOCropViewControllerDelegateImpl, _super); function TOCropViewControllerDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } TOCropViewControllerDelegateImpl.initWithOwner = function (owner) { var handler = TOCropViewControllerDelegateImpl.new(); handler._owner = owner; return handler; }; TOCropViewControllerDelegateImpl.prototype.initResolveReject = function (resolve, reject) { this._resolve = resolve; this._reject = reject; }; TOCropViewControllerDelegateImpl.prototype.cropViewControllerDidCropToImageWithRectAngle = function (cropViewController, image, cropRect, angle) { cropViewController.dismissViewControllerAnimatedCompletion(true, null); if (image) { var imgSrc = new image_source_1.ImageSource(); if (_options && _options.width && _options.height) { var rect = CGRectMake(0, 0, _options.width, _options.height); UIGraphicsBeginImageContext(rect.size); image.drawInRect(rect); var resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); try { imgSrc.setNativeSource(resizedImage); } catch (e) { console.error(e); } if (imgSrc.ios) { this._resolve({ response: "Success", image: imgSrc }); } else { this._reject({ response: "Error", image: null }); } } else { try { imgSrc.setNativeSource(image); } catch (e) { console.error(e); } if (imgSrc.ios) { this._resolve({ response: "Success", image: imgSrc }); } else { this._reject({ response: "Error", image: null }); } } } CFRelease(cropViewController.delegate); }; TOCropViewControllerDelegateImpl.prototype.cropViewControllerDidFinishCancelled = function (cropViewController, cancelled) { cropViewController.dismissViewControllerAnimatedCompletion(true, null); this._resolve({ response: "Cancelled", image: null }); CFRelease(cropViewController.delegate); }; TOCropViewControllerDelegateImpl.ObjCProtocols = [TOCropViewControllerDelegate]; return TOCropViewControllerDelegateImpl; }(NSObject)); var ImageCropper = (function () { function ImageCropper() { } ImageCropper.prototype.show = function (image, options) { return new Promise(function (resolve, reject) { _options = options; if (image.ios) { var viewController_1 = TOCropViewController.alloc().initWithImage(image.ios); var delegate = TOCropViewControllerDelegateImpl.initWithOwner(new WeakRef(viewController_1)); delegate.initResolveReject(resolve, reject); CFRetain(delegate); viewController_1.delegate = delegate; var vc = frame.topmost().ios.controller; var page = null; while (vc.presentedViewController && vc.presentedViewController.viewLoaded) { vc = vc.presentedViewController; if (!vc.beingDismissed) page = vc; } if (page === null) { page = vc; } viewController_1.customAspectRatio = CGSizeMake(_options.ratioWidth, _options.ratioHeight); viewController_1.aspectRatioLockEnabled = true; viewController_1.aspectRatioPickerButtonHidden = true; viewController_1.resetAspectRatioEnabled = false; page.presentViewControllerAnimatedCompletion(viewController_1, true, function () { if (_options) { if (_options.width && _options.height) { var gcd = ImageCropper._gcd(_options.width, _options.height); viewController_1.toolbar.clampButtonHidden = true; viewController_1.cropView.setAspectRatioAnimated(CGSizeMake(_options.width / gcd, _options.height / gcd), false); } } }); } else { reject({ response: "Error", image: null }); } }); }; ImageCropper._gcd = function (width, height) { if (height === 0) { return width; } else { return ImageCropper._gcd(height, width % height); } }; return ImageCropper; }()); exports.ImageCropper = ImageCropper;