创建category文件
.h
#import <UIKit/UIKit.h>
@interface UIView (ImageScreenShot)
-(UIImage*)imageScreenShot;
@end
.m
#import "UIView+ImageScreenShot.h"
@implementation UIView (ImageScreenShot)
-(UIImage*)imageScreenShot{
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageNew =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageNew;
}
@end
在ViewController中
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self imageScreenShot];
}
-(void)imageScreenShot{
UIImage *imageNew =[self.view imageScreenShot];
UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil);
}
@end