Skip to content

Commit

Permalink
feat: initial committ
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Mar 14, 2019
0 parents commit ff22ff0
Show file tree
Hide file tree
Showing 68 changed files with 2,216 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Hans Knöchel.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Titanium Image Cropping Tool

An easy to crop images to different scales. Based on the awesome [TOCropViewController](https://github.com/TimOliver/TOCropViewController) library. Currently iOS only (!)

<img src="./screenshot.png" height="400" alt="Example Screenshot" />

## Requirements

- [x] Titanium SDK 8.0.0+ (this module is 100 % Swift based)

## Methods

### `showCropDialog`

#### Parameters

- `image` (String, Ti.Blob, Ti.File)
- `aspectRatio` (either a dictionary {x, y} of the ratio or one of the constants * below)

## Constants

- `ASPECT_RATIO_SQUARE`
- `ASPECT_RATIO_3x2`
- `ASPECT_RATIO_5x3`
- `ASPECT_RATIO_4x3`
- `ASPECT_RATIO_5x4`
- `ASPECT_RATIO_7x5`
- `ASPECT_RATIO_16x9`

## Events

- `done`
- Attributes: `image` (Ti.Blob, if finished cropping), `cancel` (`true` if cancelled, `false` if completetd)

## Example

```js
const ImageCrop = require('ti.imagecrop')

ImageCrop.addEventListener('done', function (event) {
if (event.cancel) return;
win.add(Ti.UI.createImageView({ height: 400, image: event.image }));
});

var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});

var btn = Ti.UI.createButton({
title: 'Show crop dialog'
});

btn.addEventListener('click', function () {
ImageCrop.showCropDialog({
image: 'test.jpg'
});
});

win.add(btn);
win.open();
```

## License

MIT

## Author

Hans Knöchel
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ImageCrop = require('ti.imagecrop')

ImageCrop.addEventListener('done', function (event) {
if (event.cancel) return;
win.add(Ti.UI.createImageView({ height: 400, image: event.image }));
});

var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});

var btn = Ti.UI.createButton({
title: 'Show crop dialog'
});

btn.addEventListener('click', function () {
ImageCrop.showCropDialog({
doneButtonTitle: L('Done', 'Fertig'),
cancelButtonTitle: L('Cancel', 'Abbrechen'),
aspectRatio: ImageCrop.ASPECT_RATIO_SQUARE,
image: 'example.jpg'
});
});

win.add(btn);
win.open();
Binary file added example/example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions ios/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/build
Carthage/
ti.calendar-iphone-*.zip
/Cartfile.resolved
.DS_Store
1 change: 1 addition & 0 deletions ios/Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "TimOliver/TOCropViewController"
17 changes: 17 additions & 0 deletions ios/Classes/TiImagecrop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TiImagecrop.h
// titanium-image-crop
//
// Created by Your Name
// Copyright (c) 2019 Your Company. All rights reserved.
//

#import <UIKit/UIKit.h>

//! Project version number for TiImagecrop.
FOUNDATION_EXPORT double TiImagecropVersionNumber;

//! Project version string for TiImagecrop.
FOUNDATION_EXPORT const unsigned char TiImagecropVersionString[];

#import "TiImagecropModuleAssets.h"
107 changes: 107 additions & 0 deletions ios/Classes/TiImagecropModule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// TiImagecropModule.swift
// titanium-image-crop
//
// Created by Your Name
// Copyright (c) 2019 Your Company. All rights reserved.
//

import UIKit
import TitaniumKit

import CropViewController

@objc(TiImagecropModule)
class TiImagecropModule: TiModule {

// MARK: Public constants

@objc public let ASPECT_RATIO_SQUARE: Int = CropViewControllerAspectRatioPreset.presetSquare.rawValue
@objc public let ASPECT_RATIO_3x2: Int = CropViewControllerAspectRatioPreset.preset3x2.rawValue
@objc public let ASPECT_RATIO_5x3: Int = CropViewControllerAspectRatioPreset.preset5x3.rawValue
@objc public let ASPECT_RATIO_4x3: Int = CropViewControllerAspectRatioPreset.preset4x3.rawValue
@objc public let ASPECT_RATIO_5x4: Int = CropViewControllerAspectRatioPreset.preset5x4.rawValue
@objc public let ASPECT_RATIO_7x5: Int = CropViewControllerAspectRatioPreset.preset7x5.rawValue
@objc public let ASPECT_RATIO_16x9: Int = CropViewControllerAspectRatioPreset.preset16x9.rawValue

// MARK: Private config

private var cropViewController: CropViewController?

func moduleGUID() -> String {
return "0b0ea922-0b5a-493e-826b-596a54904a8c"
}

override func moduleId() -> String! {
return "ti.imagecrop"
}

@objc(showCropDialog:)
func showCropDialog(arguments: Array<Any>?) {
guard let params = arguments?.first as? [String: Any] else { return }
guard let image = params["image"] else {
debugPrint("[ERROR] No valid image provided")
return
}

let aspectRatio = params["aspectRatio"]
let doneButtonTitle = params["doneButtonTitle"] ?? NSLocalizedString("done", comment: "done")
let cancelButtonTitle = params["cancelButtonTitle"] ?? NSLocalizedString("cancel", comment: "done")

guard cropViewController == nil else { return }

cropViewController = CropViewController(image: TiUtils.image(image, proxy: self))

guard let cropViewController = cropViewController else { return }

cropViewController.delegate = self
cropViewController.doneButtonTitle = doneButtonTitle as? String
cropViewController.cancelButtonTitle = cancelButtonTitle as? String

// Handle both raw aspect ratios and presets (square, 16/9)
if let aspectRatio = aspectRatio {
// Handle raw values
if let aspectRatio = aspectRatio as? [String: Int] {
if let x = aspectRatio["x"], let y = aspectRatio["y"] {
cropViewController.customAspectRatio = CGSize(width: x, height: y)
}
// Handle presets
} else if let aspectRatio = aspectRatio as? Int, let rawAspectRatio = CropViewControllerAspectRatioPreset(rawValue: aspectRatio) {
cropViewController.aspectRatioPreset = rawAspectRatio
} else {
debugPrint("[ERROR] No valid aspect ratio provided!")
}
} else {
cropViewController.aspectRatioPreset = .presetSquare
}

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self._handleSingleButtonTap))
cropViewController.toolbar.doneIconButton.addGestureRecognizer(tapGestureRecognizer)

guard let controller = TiApp.controller(), let topPresentedController = controller.topPresentedController() else { return }
topPresentedController.present(cropViewController, animated: true, completion: nil)
}

@objc private func _handleSingleButtonTap() {
guard let cropViewController = cropViewController else { return }
cropViewController.dismiss(animated: true, completion: nil)
}
}

// MARK: CropViewControllerDelegate

extension TiImagecropModule : CropViewControllerDelegate {

func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
guard let blob = TiBlob()._init(withPageContext: self.pageContext, andImage: image) else { return }
self.fireEvent("done", with: ["image": blob, "cancel": false])

cropViewController.dismiss(animated: true, completion: nil)
}

func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool) {
self.fireEvent("done", with: ["cancel": true])

cropViewController.dismiss(animated: true, completion: nil)
}
}
12 changes: 12 additions & 0 deletions ios/Classes/TiImagecropModuleAssets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This is a generated file. Do not edit or your changes will be lost
*/

@interface TiImagecropModuleAssets : NSObject {

}

- (NSData *)moduleAsset;
- (NSData *)resolveModuleAsset:(NSString*)path;

@end
24 changes: 24 additions & 0 deletions ios/Classes/TiImagecropModuleAssets.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This is a generated file. Do not edit or your changes will be lost
*/
#import "TiImagecropModuleAssets.h"

extern NSData* filterDataInRange(NSData* thedata, NSRange range);

@implementation TiImagecropModuleAssets

- (NSData *)moduleAsset
{


return nil;
}

- (NSData *)resolveModuleAsset:(NSString *)path
{


return nil;
}

@end
24 changes: 24 additions & 0 deletions ios/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
Loading

0 comments on commit ff22ff0

Please sign in to comment.