Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Created November 16, 2012 16:59
Show Gist options
  • Save gfontenot/4088970 to your computer and use it in GitHub Desktop.
Save gfontenot/4088970 to your computer and use it in GitHub Desktop.
- (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