Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

actually save

  • Loading branch information...
commit ba5dfa95759f6087aea60067890520cbc3b4bb45 1 parent 37f05c5
@jasongregori authored
Showing with 83 additions and 9 deletions.
  1. +8 −1 README.markdown
  2. +1 −1  draw/AppDelegate.m
  3. +74 −7 draw/ViewController.m
View
9 README.markdown
@@ -3,4 +3,11 @@ draw-ios
You can draw images using code with this iOS project.
Just change the code in `DrawMeView`. You can use `drawRect:` or add subviews.
-When you run on the simulator, you can see the view and hit save to save it to your desktop.
+When you run on the simulator, you can see the view and hit save to save it to your desktop.
+
+
+TODO
+----
+
+1. Use a scrollview so you can zoomin on what you draw
+2. Take shadow into account when saving images
View
2  draw/AppDelegate.m
@@ -16,7 +16,7 @@ @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
- self.window = [UIWindow new];
+ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
return YES;
View
81 draw/ViewController.m
@@ -9,7 +9,15 @@
#import "ViewController.h"
#import "DrawMeView.h"
+#import <QuartzCore/QuartzCore.h>
+#import <pwd.h>
+
+@interface ViewController ()
+@property (nonatomic, weak) DrawMeView *__drawMeView;
+@end
+
@implementation ViewController
+@synthesize __drawMeView;
#pragma mark - View lifecycle
@@ -21,12 +29,14 @@ - (void)loadView {
CGSize size = v.frame.size;
v.frame = (CGRect) { CGPointMake(floor((self.view.bounds.size.width - size.width)/2.0), floor((self.view.bounds.size.height - size.height)/2.0)), size };
+ self.__drawMeView = v;
[self.view addSubview:v];
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleRightMargin);
+ [b addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
[b setTitle:@"Save" forState:UIControlStateNormal];
[b sizeToFit];
size = b.bounds.size;
@@ -34,14 +44,71 @@ - (void)loadView {
[self.view addSubview:b];
}
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-{
- // Return YES for supported orientations
- if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- } else {
- return YES;
+- (void)save {
+ NSFileManager *fm = [NSFileManager new];
+
+ static NSString *saveFolder = nil;
+ if (!saveFolder) {
+ // get desktop
+#if TARGET_IPHONE_SIMULATOR
+ NSString *logname = [NSString stringWithCString:getenv("LOGNAME") encoding:NSUTF8StringEncoding];
+ struct passwd *pw = getpwnam([logname UTF8String]);
+ NSString *home = pw ? [NSString stringWithCString:pw->pw_dir encoding:NSUTF8StringEncoding] : [@"/Users" stringByAppendingPathComponent:logname];
+ saveFolder = [NSString stringWithFormat:@"%@/Desktop", home];
+#else
+ saveFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
+#endif
+ if (![fm fileExistsAtPath:saveFolder])
+ {
+ [fm createDirectoryAtPath:saveFolder withIntermediateDirectories:NO attributes:nil error:NULL];
+ }
}
+
+ // save image
+
+ CGSize size = self.__drawMeView.bounds.size;
+
+ // TODO: include the space for the shadow
+// if (self.__drawMeView.layer.shadowOpacity > 0) {
+// size.height += fabs(self.__drawMeView.layer.shadowOffset.height);
+// size.width += fabs(self.__drawMeView.layer.shadowOffset.width);
+// }
+
+ // @1x
+ UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
+ [self.__drawMeView.layer renderInContext:UIGraphicsGetCurrentContext()];
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+ NSData *data = UIImagePNGRepresentation(image);
+
+ NSString *imagePathComponent = @"DrawMe.png";
+ int i = 0;
+ while ([fm fileExistsAtPath:[saveFolder stringByAppendingPathComponent:imagePathComponent]]) {
+ i++;
+ imagePathComponent = [NSString stringWithFormat:@"DrawMe-%i.png", i];
+ }
+
+ [fm createFileAtPath:[saveFolder stringByAppendingPathComponent:imagePathComponent]
+ contents:data
+ attributes:nil];
+
+ // @2x
+ UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
+ [self.__drawMeView.layer renderInContext:UIGraphicsGetCurrentContext()];
+ image = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+ data = UIImagePNGRepresentation(image);
+
+ imagePathComponent = @"DrawMe@2x.png";
+ i = 0;
+ while ([fm fileExistsAtPath:[saveFolder stringByAppendingPathComponent:imagePathComponent]]) {
+ i++;
+ imagePathComponent = [NSString stringWithFormat:@"DrawMe-%i@2x.png", i];
+ }
+
+ [fm createFileAtPath:[saveFolder stringByAppendingPathComponent:imagePathComponent]
+ contents:data
+ attributes:nil];
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.