Permalink
Browse files

function to do a snapshot of a view appending a title below it

  • Loading branch information...
1 parent 2246b8a commit 880227a4571d2fba03c97fd5853b220c88546c1f @ettore committed Oct 19, 2011
Showing with 100 additions and 0 deletions.
  1. +37 −0 clcg_gfx.h
  2. +63 −0 clcg_gfx.m
View
@@ -0,0 +1,37 @@
+/*
+ Copyright (c) 2011, Cubelogic. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name of Cubelogic nor the names of its contributors may be
+ used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+
+ Created by Ettore Pasquini on 10/19/11.
+
+ */
+
+#import <Foundation/Foundation.h>
+
+/**
+ * Does a snapshot of a view appending a title below it.
+ */
+UIImage *clcg_do_snapshot(UIView *v, NSString *title);
View
@@ -0,0 +1,63 @@
+/*
+ Copyright (c) 2011, Cubelogic. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+ * Neither the name of Cubelogic nor the names of its contributors may be
+ used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+
+ Created by Ettore Pasquini on 10/19/11.
+
+ */
+
+#import <QuartzCore/QuartzCore.h>
+#import "clcg_gfx.h"
+
+
+UIImage *clcg_do_snapshot(UIView *v, NSString *title)
+{
+ // make space for the title below the view
+ const CGFloat TITLE_H = 50.0;
+ CGSize sz = v.frame.size;
+ sz.height += TITLE_H;
+
+ // add a label with the title to the view (below it)
+ CGRect titlerect = CGRectMake(0, v.frame.size.height + 1, sz.width, TITLE_H - 1);
+ UILabel *l = [[UILabel alloc] initWithFrame:titlerect];
+ [l setText:title];
+ [v addSubview:l];
+ [l setNeedsLayout];
+ [l setNeedsDisplay];
+
+ // take snapshot
+ UIGraphicsBeginImageContext(sz);
+ CGContextRef ctx = UIGraphicsGetCurrentContext();
+ [v.layer renderInContext:ctx];
+ UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+
+ // cleanup
+ [l removeFromSuperview];
+ [l release];
+
+ return snapshot;
+}

0 comments on commit 880227a

Please sign in to comment.