Browse files

Adding methods to save with UTI

  • Loading branch information...
1 parent 3c1a3fe commit dec08254470cb92e5b155c90ae382fb709e53ec2 Benjamin Godard committed Feb 8, 2012
Showing with 47 additions and 8 deletions.
  1. +6 −0 Categories/UIImage+Saving.h
  2. +41 −8 Categories/UIImage+Saving.m
View
6 Categories/UIImage+Saving.h
@@ -23,14 +23,20 @@ typedef enum
@interface UIImage (NYX_Saving)
+-(BOOL)saveToURL:(NSURL*)url uti:(CFStringRef)uti backgroundFillColor:(UIColor*)fillColor;
+
-(BOOL)saveToURL:(NSURL*)url type:(NYXImageType)type backgroundFillColor:(UIColor*)fillColor;
-(BOOL)saveToURL:(NSURL*)url;
+-(BOOL)saveToPath:(NSString*)path uti:(CFStringRef)uti backgroundFillColor:(UIColor*)fillColor;
+
-(BOOL)saveToPath:(NSString*)path type:(NYXImageType)type backgroundFillColor:(UIColor*)fillColor;
-(BOOL)saveToPath:(NSString*)path;
-(BOOL)saveToPhotosAlbum;
++(NSString*)extensionForUTI:(CFStringRef)uti;
+
@end
View
49 Categories/UIImage+Saving.m
@@ -21,12 +21,15 @@ -(CFStringRef)utiForType:(NYXImageType)type;
@implementation UIImage (NYX_Saving)
--(BOOL)saveToURL:(NSURL*)url type:(NYXImageType)type backgroundFillColor:(UIColor*)fillColor
+-(BOOL)saveToURL:(NSURL*)url uti:(CFStringRef)uti backgroundFillColor:(UIColor*)fillColor
{
if (!url)
return NO;
- CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, [self utiForType:type], 1, NULL);
+ if (!uti)
+ uti = kUTTypePNG;
+
+ CGImageDestinationRef dest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, uti, 1, NULL);
if (!dest)
return NO;
@@ -40,23 +43,38 @@ -(BOOL)saveToURL:(NSURL*)url type:(NYXImageType)type backgroundFillColor:(UIColo
CFDictionaryAddValue(options, kCGImageDestinationLossyCompressionQuality, (__bridge CFNumberRef)[NSNumber numberWithFloat:1.0f]); // No compression
if (fillColor)
CFDictionaryAddValue(options, kCGImageDestinationBackgroundColor, fillColor.CGColor);
-
+
/// Add the image
CGImageDestinationAddImage(dest, self.CGImage, (CFDictionaryRef)options);
-
+
/// Write it to the destination
const bool success = CGImageDestinationFinalize(dest);
-
+
/// Cleanup
CFRelease(options);
CFRelease(dest);
-
+
return success;
}
+-(BOOL)saveToURL:(NSURL*)url type:(NYXImageType)type backgroundFillColor:(UIColor*)fillColor
+{
+ return [self saveToURL:url uti:[self utiForType:type] backgroundFillColor:fillColor];
+}
+
-(BOOL)saveToURL:(NSURL*)url
{
- return [self saveToURL:url type:NYXImageTypePNG backgroundFillColor:nil];
+ return [self saveToURL:url uti:kUTTypePNG backgroundFillColor:nil];
+}
+
+-(BOOL)saveToPath:(NSString*)path uti:(CFStringRef)uti backgroundFillColor:(UIColor*)fillColor
+{
+ if (!path)
+ return NO;
+
+ NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
+ const BOOL ret = [self saveToURL:url uti:uti backgroundFillColor:fillColor];
+ return ret;
}
-(BOOL)saveToPath:(NSString*)path type:(NYXImageType)type backgroundFillColor:(UIColor*)fillColor
@@ -65,7 +83,7 @@ -(BOOL)saveToPath:(NSString*)path type:(NYXImageType)type backgroundFillColor:(U
return NO;
NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
- const BOOL ret = [self saveToURL:url type:type backgroundFillColor:fillColor];
+ const BOOL ret = [self saveToURL:url uti:[self utiForType:type] backgroundFillColor:fillColor];
return ret;
}
@@ -93,6 +111,21 @@ -(BOOL)saveToPhotosAlbum
return ret;
}
++(NSString*)extensionForUTI:(CFStringRef)uti
+{
+ if (!uti)
+ return nil;
+
+ NSDictionary* declarations = (__bridge_transfer NSDictionary*)UTTypeCopyDeclaration(uti);
+ if (!declarations)
+ return nil;
+
+ id extensions = [(NSDictionary*)[declarations objectForKey:(__bridge NSString*)kUTTypeTagSpecificationKey] objectForKey:(__bridge NSString*)kUTTagClassFilenameExtension];
+ NSString* extension = ([extensions isKindOfClass:[NSArray class]]) ? [extensions objectAtIndex:0] : extensions;
+
+ return extension;
+}
+
#pragma mark - Private
-(CFStringRef)utiForType:(NYXImageType)type
{

0 comments on commit dec0825

Please sign in to comment.