github地址: https://github.com/mancongiOS/UIImage.git
UIImage的category
UIView+ImageScreenShot.h
#import <UIKit/UIKit.h> @interface UIView (ImageScreenShot) - (UIImage *)imageScreenShot; @end
UIView+ImageScreenShot.m
#import "UIView+ImageScreenShot.h" @implementation UIView (ImageScreenShot) - (UIImage *)imageScreenShot { UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return image;
} @end
使用:
self.view.backgroundColor = [UIColor redColor]; UIImage * image = [self.view imageScreenShot]; if (image) {
self.view.backgroundColor = [UIColor whiteColor]; self.imageView.image = image;
}