Permalink
Browse files

initial commit

  • Loading branch information...
kgn committed Mar 2, 2012
0 parents commit 60a4398291e1b62675acd6da4d839b0720cf03b2
Showing with 45 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +15 −0 NSImage+BBlock.h
  3. +21 −0 NSImage+BBlock.m
  4. +7 −0 license
  5. +1 −0 readme.md
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,15 @@
+//
+// NSImage+BBlock.h
+// BBlock
+//
+// Created by David Keegan on 2/28/12.
+// Copyright (c) 2012 David Keegan. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+@interface NSImage(BBlock)
+
++ (NSImage *)imageForSize:(NSSize)size withDrawingBlock:(void(^)())drawingBlock;
@DouglasHeriot

DouglasHeriot Nov 9, 2012

Note that as of Mountain Lion, Apple already includes a method like this! (It’s not in the documentation for some reason, but it’s in the header file, and was demonstrated at WWDC2012)

// Note that the block passed to the below method may be invoked whenever and on whatever thread the image itself is drawn on. Care should be taken to ensure that all state accessed within the drawingHandler block is done so in a thread safe manner.
+ (id)imageWithSize:(NSSize)size flipped:(BOOL)drawingHandlerShouldBeCalledWithFlippedContext drawingHandler:(BOOL (^)(NSRect dstRect))drawingHandler NS_AVAILABLE_MAC(10_8);
@kgn

kgn Nov 10, 2012

Owner

Yeah cool to see this in Foundation! In the talk I think I remember them saying they automatically cache the image without having to provide an identifier. I tried to come up with a way to do this too by creating a hash for the block but what I found was above my head :)

+
+@end
@@ -0,0 +1,21 @@
+//
+// NSImage+BBlock.m
+// BBlock
+//
+// Created by David Keegan on 2/28/12.
+// Copyright (c) 2012 David Keegan. All rights reserved.
+//
+
+#import "NSImage+BBlock.h"
+
+@implementation NSImage(BBlock)
+
++ (NSImage *)imageForSize:(NSSize)size withDrawingBlock:(void(^)())drawingBlock{
+ NSImage *image = [[NSImage alloc] initWithSize:size];
+ [image lockFocus];
+ drawingBlock();
+ [image unlockFocus];
+ return image;
+}
+
+@end
@@ -0,0 +1,7 @@
+Copyright (c) 2012 David Keegan
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
+This project is still in the very early stages and doesn't contain too much, but stay tuned :)

0 comments on commit 60a4398

Please sign in to comment.