首先寫一個(gè)UIImage的分類
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
+ (UIImage *)rendImageWithView:(UIView *)view{
// 1.開始位圖上下文
UIGraphicsBeginImageContext(view.frame.size);
// 2.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 3.截圖
[view.layer renderInContext:ctx];
// 4.獲取圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 5.關(guān)閉上下文
UIGraphicsEndImageContext() ;
return newImage;
}
@end
在需要截圖的view里調(diào)用此方法
// 1.獲取一個(gè)截圖圖片
UIImage *newImage = [ UIImage rendImageWithView:self.view];
// 2.寫入相冊
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), @"134");
#pragma mark 用來監(jiān)聽圖片保存到相冊的狀況
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error) {
[MBProgressHUD showError:@"保存失敗"];
}else{
[MBProgressHUD showSuccess:@"保存成功"];
}
NSLog(@"%@",contextInfo);
}