Last active
August 29, 2015 14:10
-
-
Save d-ronnqvist/a70e6a1623bc7da85f1d to your computer and use it in GitHub Desktop.
The formatting horrors you sometimes need to go through to fit Objective-C in a two column layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSURL *) writeImage:(NSImage *)image | |
withSceneDocumentURL:(NSURL *)documentURL | |
originalImageURL:(NSURL *)originalImageURL | |
{ | |
if (!originalImageURL) { | |
// Let the default exporter write this image | |
return nil; | |
} | |
NSString *sceneFileName = | |
[documentURL lastPathComponent]; | |
NSString *documentName = | |
[sceneFileName stringByDeletingPathExtension]; | |
NSURL *containerDirectory = | |
[documentURL URLByDeletingLastPathComponent]; | |
// The name of the folder to store the images | |
NSURL *imageFolder = | |
[containerDirectory URLByAppendingPathComponent: | |
[documentName stringByAppendingString:@"_textures"]]; | |
// Create the folder to put the images in | |
NSError *folderCreationError = nil; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL folderCreationWasSuccessful = | |
[fileManager createDirectoryAtURL:imageFolder | |
withIntermediateDirectories:NO | |
attributes:nil | |
error:&folderCreationError]; | |
// The documentation says that it's better to try and | |
// recover then to check for file existance | |
if (!folderCreationWasSuccessful && | |
folderCreationError.code != NSFileWriteFileExistsError) { | |
NSLog(@"Textures folder coudn't be created: %@", | |
[folderCreationError localizedDescription]); | |
return nil; // To signal that no image was saved | |
} | |
// Attempt to save the image | |
NSString *imageName = [originalImageURL lastPathComponent]; | |
NSURL *imageURL = | |
[imageFolder URLByAppendingPathComponent:imageName]; | |
NSData *imageData = [image TIFFRepresentation]; | |
BOOL savingImageWasSuccessful = | |
[imageData writeToFile:[imageURL path] | |
atomically:YES]; | |
if (!savingImageWasSuccessful) { | |
NSLog(@"Could not save image at URL: %@", | |
[imageURL absoluteString]); | |
return nil; // To signal that no image was saved | |
} | |
return imageURL; // The URL of the saved image | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment