Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Save images in subdirectories

Display images count for each section
  • Loading branch information...
commit fcf009c929223c16695c6019612bedce4e169fa6 1 parent d2cb77a
Cédric Luthi authored
2  Classes/AppDelegate.h
View
@@ -17,6 +17,6 @@
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
-- (NSString *) saveDirectory;
+- (NSString *) saveDirectory:(NSString *)subDirectory;
@end
6 Classes/AppDelegate.m
View
@@ -19,7 +19,7 @@ - (void) applicationDidFinishLaunching:(UIApplication *)application
[self.window addSubview:self.tabBarController.view];
}
-- (NSString *) saveDirectory
+- (NSString *) saveDirectory:(NSString *)subDirectory
{
NSString *saveDirectory = nil;
@@ -32,9 +32,11 @@ - (NSString *) saveDirectory
#else
saveDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
#endif
+ if (subDirectory)
+ saveDirectory = [saveDirectory stringByAppendingPathComponent:subDirectory];
if (![[NSFileManager defaultManager] fileExistsAtPath:saveDirectory])
- [[NSFileManager defaultManager] createDirectoryAtPath:saveDirectory withIntermediateDirectories:NO attributes:nil error:NULL];
+ [[NSFileManager defaultManager] createDirectoryAtPath:saveDirectory withIntermediateDirectories:YES attributes:nil error:NULL];
return saveDirectory;
}
5 Classes/ArtworkDetailViewController.h
View
@@ -13,11 +13,10 @@
IBOutlet UIBarButtonItem *saveButton;
IBOutlet UIImageView *imageView;
- UIImage *image;
- NSString *name;
+ NSDictionary *imageInfo;
}
-- (id) initWithImage:(UIImage *)anImage name:(NSString *)aName;
+- (id) initWithImageInfo:(NSDictionary *)anImageInfo;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *saveButton;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
27 Classes/ArtworkDetailViewController.m
View
@@ -10,24 +10,23 @@
@interface ArtworkDetailViewController ()
-@property (nonatomic, retain) UIImage *image;
-@property (nonatomic, retain) NSString *name;
+@property (nonatomic, retain) NSDictionary *imageInfo;
+@property (nonatomic, readonly) UIImage *image;
+@property (nonatomic, readonly) NSString *name;
@end
@implementation ArtworkDetailViewController
@synthesize saveButton, imageView;
-@synthesize image;
-@synthesize name;
+@synthesize imageInfo;
-- (id) initWithImage:(UIImage *)anImage name:(NSString *)aName
+- (id) initWithImageInfo:(NSDictionary *)anImageInfo
{
if (!(self = [super initWithNibName:@"ArtworkDetailViewController" bundle:nil]))
return nil;
- self.image = anImage;
- self.name = aName;
+ self.imageInfo = anImageInfo;
return self;
}
@@ -36,11 +35,20 @@ - (void) dealloc
{
self.saveButton = nil;
self.imageView = nil;
- self.image = nil;
- self.name = nil;
+ self.imageInfo = nil;
[super dealloc];
}
+- (UIImage *) image
+{
+ return [self.imageInfo objectForKey:@"image"];
+}
+
+- (NSString *) name
+{
+ return [self.imageInfo objectForKey:@"name"];
+}
+
- (void) viewWillAppear:(BOOL)animated
{
self.title = [self.name stringByDeletingPathExtension];
@@ -70,7 +78,6 @@ - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (IBAction) save
{
id artworkViewController = [self.navigationController.viewControllers objectAtIndex:0];
- NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.image, @"image", self.name, @"name", nil];
[artworkViewController performSelector:@selector(saveImage:) withObject:imageInfo];
}
54 Classes/ArtworkViewController.m
View
@@ -146,12 +146,12 @@ - (void) addImage:(UIImage *)image filePath:(NSString *)filePath
{
NSString *fileName = [filePath lastPathComponent];
NSString *bundlePath = [filePath stringByDeletingLastPathComponent];
- NSString *bundleName = [[bundlePath lastPathComponent] stringByDeletingPathExtension];
+ NSString *bundleName = [bundlePath lastPathComponent];
if ([bundleName length] == 0) // Extracted from .artwork file, has no actual path
- bundleName = @" Artwork"; // With a space so that it's the first section
+ bundleName = [self isEmoji] ? @"Emoji" : @"Shared";
// We already have higher resolution emoji
- if ([bundleName isEqualToString:@"WebCore"] && [fileName hasPrefix:@"emoji"])
+ if ([bundleName isEqualToString:@"WebCore.framework"] && [fileName hasPrefix:@"emoji"])
return;
for (UITableViewCell *cell in [self allCells])
@@ -162,15 +162,15 @@ - (void) addImage:(UIImage *)image filePath:(NSString *)filePath
NSData *file2Data = [NSData dataWithContentsOfFile:filePath];
if ([file1Data isEqualToData:file2Data]) // Filter out exact duplicates
return;
-
- // Avoid duplicate file names so that "Save All" does not clobber any file
- fileName = [bundleName stringByAppendingFormat:@"_%@", fileName];
}
}
// There are only a few settings bundles, so group them
if ([bundleName rangeOfString:@"Settings"].location != NSNotFound)
+ {
+ fileName = [NSString stringWithFormat:@"%@_%@", [bundleName stringByDeletingPathExtension], fileName];
bundleName = @"Settings";
+ }
if (![self.bundles objectForKey:bundleName])
[self.bundles setObject:[NSMutableArray array] forKey:bundleName];
@@ -247,12 +247,18 @@ - (void) saveImage:(NSDictionary *)imageInfo
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIImage *image = [imageInfo objectForKey:@"image"];
NSString *imageName = [imageInfo objectForKey:@"name"];
- NSString *imagePath = [[appDelegate saveDirectory] stringByAppendingPathComponent:pathWithScale(imageName, [image scale])];
+ NSString *bundleName = [[imageInfo objectForKey:@"bundleName"] stringByReplacingOccurrencesOfString:@"." withString:@" "];
+ NSString *imagePath = [[appDelegate saveDirectory:bundleName] stringByAppendingPathComponent:pathWithScale(imageName, [image scale])];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
[self performSelectorOnMainThread:@selector(incrementSaveCounter) withObject:nil waitUntilDone:YES];
[pool drain];
}
+- (NSArray *) sectionKeys
+{
+ return [[self.bundles allKeys] sortedArrayUsingSelector:@selector(localizedCompare:)];
+}
+
- (IBAction) saveAll
{
self.saveCounter = 0;
@@ -260,12 +266,15 @@ - (IBAction) saveAll
self.saveAllButton.enabled = NO;
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue setMaxConcurrentOperationCount:4];
- for (UITableViewCell *cell in [self allCells])
+ for (NSString *bundleName in [self sectionKeys])
{
- NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:((UIImageView*)cell.accessoryView).image, @"image", cell.textLabel.text, @"name", nil];
- NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(saveImage:) object:imageInfo];
- [queue addOperation:operation];
- [operation release];
+ for (UITableViewCell *cell in [self.bundles objectForKey:bundleName])
+ {
+ NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:((UIImageView*)cell.accessoryView).image, @"image", cell.textLabel.text, @"name", bundleName, @"bundleName", nil];
+ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(saveImage:) object:imageInfo];
+ [queue addOperation:operation];
+ [operation release];
+ }
}
}
@@ -294,12 +303,19 @@ - (NSArray *) filteredCells
- (NSArray *) sectionTitles
{
- return [[self.bundles allKeys] sortedArrayUsingSelector:@selector(localizedCompare:)];
+ NSMutableArray *sectionTitles = [NSMutableArray array];
+ for (NSString *bundleName in [self sectionKeys])
+ {
+ NSArray *cells = [self.bundles objectForKey:bundleName];
+ NSString *sectionTitle = [NSString stringWithFormat:@"%@ (%d)", bundleName, [cells count]];
+ [sectionTitles addObject:sectionTitle];
+ }
+ return sectionTitles;
}
- (NSArray *) cellsInSection:(NSUInteger)section
{
- NSString *bundleName = [[self sectionTitles] objectAtIndex:section];
+ NSString *bundleName = [[self sectionKeys] objectAtIndex:section];
return [self.bundles objectForKey:bundleName];
}
@@ -358,9 +374,9 @@ - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
- UIImage *image = ((UIImageView*)cell.accessoryView).image;
- NSString *imageName = cell.textLabel.text;
- ArtworkDetailViewController *artworkDetailViewController = [[ArtworkDetailViewController alloc] initWithImage:image name:imageName];
+ NSString *bundleName = [[self sectionKeys] objectAtIndex:indexPath.section];
+ NSDictionary *imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:((UIImageView*)cell.accessoryView).image, @"image", cell.textLabel.text, @"name", bundleName, @"bundleName", nil];
+ ArtworkDetailViewController *artworkDetailViewController = [[ArtworkDetailViewController alloc] initWithImageInfo:imageInfo];
[self.navigationController pushViewController:artworkDetailViewController animated:YES];
[artworkDetailViewController release];
}
@@ -386,9 +402,9 @@ - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)contr
{
NSUInteger section = 0;
NSUInteger row = 0;
- for (NSString *title in [self sectionTitles])
+ for (NSString *bundleName in [self sectionKeys])
{
- NSArray *cells = [self.bundles objectForKey:title];
+ NSArray *cells = [self.bundles objectForKey:bundleName];
row = [cells indexOfObject:firstVisibleCell];
if (row != NSNotFound)
break;
2  Classes/GlossyButtonViewController.m
View
@@ -148,7 +148,7 @@ - (void) saveButtonInState:(UIControlState)state
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
NSData *data = UIImagePNGRepresentation(image);
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
- [data writeToFile:[[appDelegate saveDirectory] stringByAppendingPathComponent:buttonName] atomically:YES];
+ [data writeToFile:[[appDelegate saveDirectory:@"UIGlassButton"] stringByAppendingPathComponent:buttonName] atomically:YES];
UIGraphicsEndImageContext();
Please sign in to comment.
Something went wrong with that request. Please try again.