Skip to content

Instantly share code, notes, and snippets.

@rollsroyc3
Created March 23, 2016 20:54
Show Gist options
  • Save rollsroyc3/76ef5345bca0c9361400 to your computer and use it in GitHub Desktop.
Save rollsroyc3/76ef5345bca0c9361400 to your computer and use it in GitHub Desktop.
Rotate and save image using Appcelerator Titanium
/*
https://www.snip2code.com/Snippet/78042/Rotate-and-save-image-using-Appcelerator
Hi, I have tested this issue in Ti SDK 3.3.0.RC. Its working good.
Testing Environment:
Titanium SDK: 3.3.0.RC, 3.2.3.GA
Titanium CLI: 3.3.0-rc
Android Version 4.4.3 KitKat
iOS Version 7.1
Appcelerator Studio, build: 3.3.0.201406271159
Step to Reproduce
Create a sample Ti Classic project from AppC Studio
Update app.js file with test code
Run on Android device and iOS Simulator
Rotate and save image is working.
*/
Ti.UI.setBackgroundColor('#000');
var win1 = Ti.UI.createWindow({
backgroundColor: '#fff',
});
var angle = {
size: 224,
old: 0,
diff: 0,
adj: 0,
get: function (center, p1) {
var p0 = {x: center.x, y: center.y - Math.sqrt(Math.abs(p1.x - center.x) * Math.abs(p1.x - center.x)
+ Math.abs(p1.y - center.y) * Math.abs(p1.y - center.y))};
return (2 * Math.atan2(p1.y - p0.y, p1.x - p0.x)) * 180 / Math.PI;
}
};
var img = Ti.UI.createImageView({
height: angle.size,
image: '1.jpg',
width: angle.size
});
win1.add(img);
var view = Ti.UI.createImageView({
//backgroundColor: '#99000000',
image: '1.jpg',
height: angle.size,
width: angle.size
});
win1.add(view);
view.addEventListener('touchstart', function (e) {
var point = { x: e.x - (angle.size / 2), y: e.y - (angle.size / 2) };
var newAngle = Math.floor(angle.get({ x: 0, y: 0 }, point));
angle.diff = (newAngle - angle.old);
});
view.addEventListener('touchmove', function (e) {
var point = { x: e.x - (angle.size / 2), y: e.y - (angle.size / 2) };
var newAngle = Math.floor(angle.get({ x: 0, y: 0 }, point));
angle.adj = Math.abs((newAngle - angle.diff) % 360);
var _transform = Ti.UI.create2DMatrix().rotate(angle.adj);
img.transform = _transform;
});
view.addEventListener('touchend', function (e) {
angle.old = angle.adj;
});
win1.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment