Skip to content

Commit

Permalink
Do not save images with @2x suffix when they are not @2x
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Aug 5, 2010
1 parent 2692594 commit 2334746
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 9 additions & 7 deletions Classes/ArtworkViewController.m
Expand Up @@ -29,9 +29,8 @@
return [systemFrameworksPath stringByDeletingLastPathComponent];
}

static NSString *pathWithScale(NSString *path)
static NSString *pathWithScale(NSString *path, CGFloat scale)
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1)
return [[[path stringByDeletingPathExtension] stringByAppendingFormat:@"@%gx", scale] stringByAppendingPathExtension:[path pathExtension]];
else
Expand All @@ -41,9 +40,11 @@
// Workaround http://www.openradar.me/8225750
static UIImage *imageWithContentsOfFile(NSString *path)
{
if ([[NSFileManager defaultManager] fileExistsAtPath:pathWithScale(path)])
path = pathWithScale(path);
return [UIImage imageWithContentsOfFile:path];
NSString *imagePathWithScale = pathWithScale(path, [[UIScreen mainScreen] scale]);
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePathWithScale])
return [UIImage imageWithContentsOfFile:imagePathWithScale];
else
return [UIImage imageWithContentsOfFile:path];
}


Expand Down Expand Up @@ -244,9 +245,10 @@ - (void) saveImage:(NSDictionary *)imageInfo
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIImage *image = [imageInfo objectForKey:@"image"];
NSString *imageName = [imageInfo objectForKey:@"name"];
NSString *imagePath = [[appDelegate saveDirectory] stringByAppendingPathComponent:pathWithScale(imageName)];
[UIImagePNGRepresentation([imageInfo objectForKey:@"image"]) writeToFile:imagePath atomically:YES];
NSString *imagePath = [[appDelegate saveDirectory] stringByAppendingPathComponent:pathWithScale(imageName, [image scale])];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
[self performSelectorOnMainThread:@selector(incrementSaveCounter) withObject:nil waitUntilDone:YES];
[pool drain];
}
Expand Down
4 changes: 4 additions & 0 deletions UIKit_Artwork_Extractor_Prefix.pch
Expand Up @@ -10,6 +10,10 @@
@interface UIScreen (iOS4)
@property(nonatomic,readonly) CGFloat scale;
@end

@interface UIImage (iOS4)
@property(nonatomic,readonly) CGFloat scale;
@end
#endif

#endif
8 changes: 5 additions & 3 deletions main.m
Expand Up @@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

CGFloat UIScreen_scale(id self, SEL _cmd)
CGFloat iOS3_scale(id self, SEL _cmd)
{
return 1.0;
}
Expand All @@ -18,10 +18,12 @@ int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// -[UIView alpha] has the same method signature as -[UIScreen scale]
// -[UIView alpha] has the same method signature as -[UIScreen/UIImage scale]
Method alpha = class_getInstanceMethod([UIView class], @selector(alpha));
if (![UIScreen instancesRespondToSelector:@selector(scale)])
class_addMethod([UIScreen class], @selector(scale), (IMP)UIScreen_scale, method_getTypeEncoding(alpha));
class_addMethod([UIScreen class], @selector(scale), (IMP)iOS3_scale, method_getTypeEncoding(alpha));
if (![UIImage instancesRespondToSelector:@selector(scale)])
class_addMethod([UIImage class], @selector(scale), (IMP)iOS3_scale, method_getTypeEncoding(alpha));

int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
Expand Down

0 comments on commit 2334746

Please sign in to comment.