Created
November 16, 2012 16:59
-
-
Save gfontenot/4088970 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError | |
{ | |
BOOL status = NO; | |
CGImageDestinationRef dest = nil; | |
/* Create a new CFStringRef containing a uniform type identifier (UTI) that is the equivalent | |
of the passed file extension. */ | |
CFStringRef utiRef = UTTypeCreatePreferredIdentifierForTag( | |
kUTTagClassFilenameExtension, | |
(CFStringRef) typeName, | |
kUTTypeData | |
); | |
if (utiRef==nil) | |
{ | |
goto bail; | |
} | |
/* Create an image destination writing to absoluteURL. */ | |
dest = CGImageDestinationCreateWithURL((CFURLRef)absoluteURL, utiRef, 1, nil); | |
CFRelease(utiRef); | |
if (dest==nil) | |
{ | |
goto bail; | |
} | |
/* The image snapshot associated with the document. */ | |
CGImageRef docImage = [self currentCGImage]; | |
if (docImage==nil) | |
{ | |
goto bail; | |
} | |
/* Set the image in the image destination to be our document image snapshot. */ | |
CGImageDestinationAddImage(dest, docImage, NULL); | |
/* Writes image data to the URL associated with the image destination. */ | |
status = CGImageDestinationFinalize(dest); | |
bail: | |
if (dest) | |
{ | |
CFRelease(dest); | |
} | |
return status; | |
} | |
- (BOOL)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError | |
{ | |
NSString *docTypeName = [absoluteURL pathExtension]; | |
/* Was an extension specified? */ | |
if ([docTypeName isEqualToString:@""]) | |
{ | |
/* No extension, so use jpeg. */ | |
docTypeName = @"jpg"; | |
return ([self writeToURL:[absoluteURL URLByAppendingPathExtension:docTypeName] ofType:docTypeName error:outError]); | |
} | |
return ([self writeToURL:absoluteURL ofType:docTypeName error:outError]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment