Skip to content

Commit

Permalink
Made saving of app icons fail more gracefully.
Browse files Browse the repository at this point in the history
q.v. #33
  • Loading branch information
alunbestor committed Dec 17, 2013
1 parent 45835cb commit 40602a1
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions Other Sources/ADBToolkit/NSImage+ADBSaveImages.m
Expand Up @@ -55,8 +55,6 @@ - (BOOL) saveToURL: (NSURL *)URL
- (BOOL) saveAsIconToURL: (NSURL *)URL
error: (out NSError **)outError
{
NSUInteger numImages = self.representations.count;

if ([URL checkResourceIsReachableAndReturnError: NULL])
{
if (outError)
Expand All @@ -68,26 +66,57 @@ - (BOOL) saveAsIconToURL: (NSURL *)URL
return NO;
}

NSUInteger numImages = self.representations.count;
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)URL,
kUTTypeAppleICNS,
numImages,
NULL);

for (NSImageRep *rep in self.representations)
if (destination != NULL)
{
for (NSImageRep *rep in self.representations)
{
CGImageRef image = [rep CGImageForProposedRect: NULL context: nil hints: nil];
NSDictionary* properties = @{(NSString *)kCGImagePropertyDPIWidth: @(rep.size.width),
(NSString *)kCGImagePropertyDPIHeight: @(rep.size.height),
(NSString *)kCGImagePropertyPixelWidth: @(rep.pixelsWide),
(NSString *)kCGImagePropertyPixelHeight: @(rep.pixelsHigh),
};

CGImageDestinationAddImage(destination, image, (__bridge CFDictionaryRef)properties);
}

BOOL finalized = CGImageDestinationFinalize(destination);
CFRelease(destination);

if (finalized)
{
return YES;
}
else
{
//TODO: try to get more specific error information out of Core Graphics
if (outError)
{
*outError = [NSError errorWithDomain: NSCocoaErrorDomain
code: NSFileWriteUnknownError
userInfo: @{ NSURLErrorKey: URL }];
}
return NO;
}
}
else
{
CGImageRef image = [rep CGImageForProposedRect: NULL context: nil hints: nil];
NSDictionary* properties = @{(NSString *)kCGImagePropertyDPIWidth: @(rep.size.width),
(NSString *)kCGImagePropertyDPIHeight: @(rep.size.height),
(NSString *)kCGImagePropertyPixelWidth: @(rep.pixelsWide),
(NSString *)kCGImagePropertyPixelHeight: @(rep.pixelsHigh),
};
//TODO: try to get more specific error information out of Core Graphics
if (outError)
{
*outError = [NSError errorWithDomain: NSCocoaErrorDomain
code: NSFileWriteUnknownError
userInfo: @{ NSURLErrorKey: URL }];
}

CGImageDestinationAddImage(destination, image, (__bridge CFDictionaryRef)properties);
return NO;
}

BOOL success = CGImageDestinationFinalize(destination);
CFRelease(destination);
return success;
}

@end

0 comments on commit 40602a1

Please sign in to comment.