Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active December 14, 2015 21:28
Show Gist options
  • Select an option

  • Save monjer/5151183 to your computer and use it in GitHub Desktop.

Select an option

Save monjer/5151183 to your computer and use it in GitHub Desktop.
//
// UIView+Screenshot.h
//
// Created by manjun.han on 13-3-13.
// Copyright (c) 2013年 com.hmj. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (Screenshot)
- (UIImage*)screenshot ;
@end
//
// UIView+Screenshot.m
//
// Created by manjun.han on 13-3-13.
// Copyright (c) 2013年 com.hmj. All rights reserved.
//
#import "UIView+Screenshot.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (Screenshot)
- (UIImage*)screenshot
{
// 如果bitmap是彻底不透明的,设置opaque = YES,来优化bitmap的存储。貌似生成的bitmap会忽略alpha通道值
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext() ;
[self.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment