ios如何截取屏幕并保存为图片

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
UIGraphicsBeginImageContext(self.view.bounds.size); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小为
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片
UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
UIGraphicsBeginImageContext(self.view.bounds.size); //currentView 当前的view 创建一个基于位图的图形上下文并指定大小为 [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片 UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文 UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图
UIGraphicsBeginImageContext(self.view.bounds.size);     //currentView 当前的view  创建一个基于位图的图形上下文并指定大小为  
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//renderInContext呈现接受者及其子范围到指定的上下文  
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();//返回一个基于当前图形上下文的图片     
UIGraphicsEndImageContext();//移除栈顶的基于当前位图的图形上下文    
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);//然后将该图片保存到图片图

或者:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
+ (UIImage *)captureImageFromView:(UIView *)view {
CGRect screenRect = [view bounds];
UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)captureImageFromView:(UIView *)view { CGRect screenRect = [view bounds]; UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); [view.layer renderInContext:ctx]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
+ (UIImage *)captureImageFromView:(UIView *)view {
    CGRect screenRect = [view bounds];
    UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *