Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Created November 21, 2018 06:14
Show Gist options
  • Save MotiurRahman/dbe8af54f0b3ba02f0d37bcf4899111c to your computer and use it in GitHub Desktop.
Save MotiurRahman/dbe8af54f0b3ba02f0d37bcf4899111c to your computer and use it in GitHub Desktop.
Take Screenshot
// -- General UI -- //
var win = Ti.UI.createWindow({
backgroundColor: "#fff",
layout:"vertical"
});
var btn = Ti.UI.createButton({
title: "Open Camera",
top: 50
});
var pic = Ti.UI.createImageView({
width: Ti.UI.FILL,
height: 400,
top: 20,
backgroundColor: "#ccc"
});
// -- Overlay UI -- //
var overlay = Ti.UI.createView();
var takePictureButton = Ti.UI.createButton({
bottom: 10,
right: 10,
title: "Take ScreenShot!",
backgroundColor: "#ff0",
width: 150,
height: 40
});
var hideCameraButton = Ti.UI.createButton({
bottom: 10,
left: 10,
title: "Close Camera!",
backgroundColor: "#00f",
width: 150,
height: 40
});
takePictureButton.addEventListener("click", function() {
Ti.Media.takeScreenshot(function(e) {
Ti.API.info(e.media);
pic.image = e.media;
Ti.Media.hideCamera();
});
});
hideCameraButton.addEventListener("click", function() {
Ti.Media.hideCamera();
});
overlay.add(takePictureButton);
overlay.add(hideCameraButton);
btn.addEventListener("click", function() {
// check if we already have permissions to capture media
if (!Ti.Media.hasCameraPermissions()) {
Ti.API.info('Going for permissiond');
// request permissions to capture media
Ti.Media.requestCameraPermissions(function(e) {
Ti.API.info('permission granted');
// success! we can capture media!
if (e.success) {
Ti.Media.showCamera({
overlay: overlay,
showControls: false,
autohide: false,
success: function(e) {
pic.image = e.media;
Ti.Media.hideCamera();
},
cancel: function(e) {
Ti.API.warn("Cancelled: " + JSON.stringify(e));
},
error: function(e) {
Ti.API.error("Error: " + JSON.stringify(e));
},
saveToPhotoGallery: true,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO]
});
// oops! could not obtain required permissions...
} else {
Ti.API.error('could not obtain camera permissions!');
}
});
} else {
Ti.API.info('already permissiond');
// yay! we already have permissions!
Ti.Media.showCamera({
overlay: overlay,
showControls: false,
autohide: false,
success: function(e) {
pic.image = e.media;
Ti.Media.hideCamera();
},
cancel: function(e) {
Ti.API.warn("Cancelled: " + JSON.stringify(e));
},
error: function(e) {
Ti.API.error("Error: " + JSON.stringify(e));
},
saveToPhotoGallery: true,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
});
}
});
win.add(btn);
win.add(pic);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment