GithubHelp home page GithubHelp logo

sahwar / psdwriter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bengotow/psdwriter

0.0 1.0 0.0 620 KB

An Objective-C library for creating layered PSDs from a set of images on Mac OS X or iOS

License: Other

Objective-C 100.00%

psdwriter's Introduction

PSDWriter

A few years ago, I wrote an app called Layers for iPhone and iPad. It was the first painting app for iPhone to support multiple layers, and one of it's killer features was the ability to email drawings as layered PSDs. Many people have asked me about PSD functionality over the years and I'm excited to release this code to the community. If you find it useful, please let me know and consider mentioning PSDWriter on your app's about screen. Though this code probably looks straightforward, it took forever to get working. To this day, I can still read PSD files by hand as hex.

Note: This library only writes PSDs, it does not read them. Because there are many versions of the PSD spec, and newer versions have a gazillion features, it's much easier to write a simple, layered PSD file than it is to read PSD. I have no plans to read PSD files anytime soon.

Write a PSD file (iOS):

// grab the two images that we'll use
UIImage * image1 = [UIImage imageNamed:@"layer1.png"];
UIImage * image2 = [UIImage imageNamed:@"layer2.png"];

// Initialize a new PSD writer with the desired canvas size
PSDWriter * w = [[PSDWriter alloc] initWithDocumentSize: CGSizeMake(900, 900)];

// Add our first layer
[w addLayerWithCGImage:[image1 CGImage] andName:@"Island Layer" andOpacity:1 andOffset:CGPointMake(500, 700)];

// Add our second layer
[w addLayerWithCGImage:[image2 CGImage] andName:@"Apple Layer" andOpacity:1 andOffset:CGPointMake(500, 700)];

// Create the PSD data
NSData * psd = [w createPSDData];


// write the PSD data to disk (in the simulator, this will put it on the root level
// of your boot volume). On the actual device, we'd have to email it or something
[psd writeToFile:@"/output.psd" atomically:NO];

// clean up!
[w release];

Write a PSD file (Mac OS X):

// grab the two images that we'll use
NSImage * layer1 = [[[NSImage alloc] initWithContentsOfFile: @"layer1.png"] autorelease];
NSImage * layer2 = [[[NSImage alloc] initWithContentsOfFile: @"layer2.png"] autorelease];
if (!layer1 || !layer2) {
    NSLog(@"Example will fail - images missing!");
}

// Initialize a new PSD writer with the desired canvas size
PSDWriter * w = [[PSDWriter alloc] initWithDocumentSize: CGSizeMake(900, 900)];

// Add our first layer
CGImageRef layer1CG = [self newCGImageForNSImage: layer1];
[w addLayerWithCGImage:layer1CG andName:@"Island Layer" andOpacity:1 andOffset:CGPointZero];
CGImageRelease(layer1CG);

// Add our second layer
CGImageRef layer2CG = [self newCGImageForNSImage: layer2];
[w addLayerWithCGImage:layer2CG andName:@"Apple Layer" andOpacity:0.5 andOffset:CGPointMake(100, 100)];
CGImageRelease(layer2CG);

// Create the PSD data
NSData * psd = [w createPSDData];

// write the PSD data to disk and then open the image in Photoshop
[psd writeToFile:@"./output.psd" atomically:NO];
[[NSWorkspace sharedWorkspace] openFile: @"./output.psd"];

// clean up!
[w release];

Going Further

This is a pretty basic implementation that I created for the Layers app. If you extend the library to do more, submit a pull request!

If you're interested in learning more about the PSD file format or need a copy of the PSD spec to extend PSDWriter, check out the documentation on Adobe's website. A word of wisdom: implement the oldest version of the PSD file format that supports what you need. Each successive version seems to be more complicated than the last. PSDWriter was written using the 2003 spec, which is much simpler than the current version.

psdwriter's People

Contributors

bengotow avatar bryant1410 avatar flemingm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.