Skip to content

Instantly share code, notes, and snippets.

@christophercotton
christophercotton / CGRectAspectFit.m
Created November 20, 2015 14:51 — forked from lanephillips/CGRectAspectFit.m
Objective-C code to fit a CGRect inside or outside another CGRect while maintaining aspect ratio. The fitted rectangle is centered on the target rectangle.
CGFloat ScaleToAspectFitRectInRect(CGRect rfit, CGRect rtarget)
{
// first try to match width
CGFloat s = CGRectGetWidth(rtarget) / CGRectGetWidth(rfit);
// if we scale the height to make the widths equal, does it still fit?
if (CGRectGetHeight(rfit) * s <= CGRectGetHeight(rtarget)) {
return s;
}
// no, match height instead
return CGRectGetHeight(rtarget) / CGRectGetHeight(rfit);
@christophercotton
christophercotton / App icons.jstalk
Last active December 30, 2015 00:59 — forked from douglashill/App icons.jstalk
Modified the gist to allow it to work with the front most document.
function main(image, doc, layer) {
var sizes = [29, 40, 50, 57, 58, 72, 76, 80, 100, 114, 120, 144, 152, 1024];
for (var idx = 0; idx < sizes.length; idx++) {
doc.scaleImageToWidth(sizes[idx]);
doc.dataRepresentationOfType("public.png").writeToFile("/Users/cotton/Desktop/icon-" + sizes[idx] + ".png");
doc.undo();
}
}