Skip to content

Instantly share code, notes, and snippets.

@ptmt
Last active Aug 24, 2016
Embed
What would you like to do?
node --harmony utils/generate_images.js
'use strict';
const IMAGES_DIR = './images'
const ASSETS_DIR = './iOS/Images.xcassets'
const EXTENSIONS = ['jpg', 'jpeg', 'png']
var fs = require('fs')
var gm = require('gm')
function convertAndCopy(source, scaleFactor, imageName, imageExt) {
let per = scaleFactor === 3 ? '100%' : scaleFactor === 2 ? '50%' : '25%'
let newName = imageName + '@' + scaleFactor + 'x.' + imageExt
let target = ASSETS_DIR + '/' + imageName + '.imageset/' + newName
gm(source)
.resize(per)
.write(target, (err) => {
if (!err) {
console.log(target, 'has been created')
}
})
return newName
}
fs.readdirSync(IMAGES_DIR)
.filter(f=> EXTENSIONS.filter(e=> f.indexOf(e)))
.forEach(image => {
let imageName = image.split('.')[0];
let imageExt = image.split('.')[1];
// create a directory
try {
fs.mkdirSync(ASSETS_DIR + '/' + imageName + '.imageset')
} catch (e) {
// already exist
}
// generate json
let contentsJson = fs.readFileSync('./utils/Contents.json', {encoding: 'utf8'})
let tasks = []
var factors = [1, 2, 3];
factors.forEach(scaleFactor => {
let newFilename = convertAndCopy(IMAGES_DIR + '/' + image, scaleFactor, imageName, imageExt)
contentsJson = contentsJson.replace('{FILENAME' + scaleFactor + '}', newFilename)
})
fs.writeFileSync(ASSETS_DIR + '/' + imageName + '.imageset/Contents.json', contentsJson)
})
console.log('Please rebuild the XCode project!')
@lukasreichart

This comment has been minimized.

Copy link

@lukasreichart lukasreichart commented May 12, 2015

Thanks a lot. Works very well.

@davemo

This comment has been minimized.

Copy link

@davemo davemo commented May 13, 2015

What version of node are you running this on? And which version of the gm dependency are you relying on?

I get a syntax error with node 0.12.2:

> node --harmony ./script/generate_xcode_assets.js

/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:269
      proc.stdout.on('data', onOut = function (data) {
                 ^
TypeError: Cannot read property 'on' of undefined
    at gm._spawn (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:269:18)
    at /Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:109:12
    at series (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/node_modules/array-series/index.js:11:36)
    at gm._preprocess (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:185:5)
    at gm.write (/Users/davidmosher/code/hsDeckBrowser/node_modules/gm/lib/command.js:107:10)
    at convertAndCopy (/Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:16:6)
    at /Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:44:25
    at Array.forEach (native)
    at /Users/davidmosher/code/hsDeckBrowser/script/generate_xcode_assets.js:43:13
    at Array.forEach (native)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment