Skip to content

Instantly share code, notes, and snippets.

@chrismiles
Created June 9, 2011 09:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrismiles/1016407 to your computer and use it in GitHub Desktop.
Save chrismiles/1016407 to your computer and use it in GitHub Desktop.
UIView class extension with some of my handy convenience methods
//
// UIView+CMExtras.h
//
// Created by Chris Miles on 9/06/11.
// Copyright 2011 Chris Miles.
//
// 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.
//
#import <UIKit/UIKit.h>
@interface UIView ( CMExtras )
/* Snapshot the view to a UIImage */
- (UIImage *)snapshotImage;
- (UIImage *)snapshotImageOpaque:(BOOL)opaque;
/* Flatten and snapshot the view to a PNG file. */
- (void)snapshotToPNGFilePath:(NSString *)filePath opaque:(BOOL)opaque;
/* Find first subview that is a subclass of specified class, optionally recursing the whole subview tree */
- (UIView *)findSubviewWithKindOfClass:(Class)class recursive:(BOOL)recursive;
@end
//
// UIView+CMExtras.m
//
// Created by Chris Miles on 9/06/11.
// Copyright 2011 Chris Miles.
//
// 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.
//
#import <QuartzCore/QuartzCore.h>
#import "UIView+CMExtras.h"
@implementation UIView ( CMExtras )
- (UIImage *)snapshotImageOpaque:(BOOL)opaque
{
UIImage *snapshotImage = nil;
CGFloat scale = 1.0;
UIScreen *mainScreen = [UIScreen mainScreen];
if ([mainScreen respondsToSelector:@selector(scale)]) {
scale = [mainScreen scale];
}
if (UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, opaque, scale);
}
else {
UIGraphicsBeginImageContext(self.bounds.size);
}
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect cropRect = CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds)*scale, CGRectGetHeight(self.bounds)*scale);
CGImageRef croppedImage = CGImageCreateWithImageInRect(viewImage.CGImage, cropRect);
snapshotImage = [UIImage alloc];
if ([snapshotImage respondsToSelector:@selector(initWithCGImage:scale:orientation:)]) {
snapshotImage = [[snapshotImage initWithCGImage:croppedImage scale:scale orientation:UIImageOrientationUp] autorelease];
}
else {
snapshotImage = [[snapshotImage initWithCGImage:croppedImage] autorelease];
}
CGImageRelease(croppedImage);
return snapshotImage;
}
- (UIImage *)snapshotImage
{
return [self snapshotImageOpaque:NO];
}
- (void)snapshotToPNGFilePath:(NSString *)filePath opaque:(BOOL)opaque
{
UIImage *image = [self snapshotImageOpaque:opaque];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
}
- (UIView *)findSubviewWithKindOfClass:(Class)class recursive:(BOOL)recursive
{
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:class]) {
return subview;
}
if (recursive) {
UIView *result = [subview findSubviewWithKindOfClass:class recursive:recursive];
if (result) {
return result;
}
}
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment