- (UIImage *) imageWithView1:(UIView *)view{
UIImage *img;
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
-(UIImage *)GetScreenShotWithScreen:(UIView *)Screen andFrame:(CGRect)frame
{
UIGraphicsBeginImageContext(Screen.frame.size);
[Screen.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * shotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef shotRef = shotImage.CGImage;
CGImageRef ResultRef = CGImageCreateWithImageInRect(shotRef, frame);
UIImage * Result = [UIImage imageWithCGImage:ResultRef];
CGImageRelease(ResultRef);
return Result;
}
//获得某个范围内的屏幕图像
- (UIImage *)imageFromView: (UIView *) theView atFrame:(CGRect)r
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIRectClip(r);
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;//[self getImageAreaFromImage:theImage atFrame:r];
}
UIImage *img;
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
截屏模糊问题
这个方法截屏会模糊
UIGraphicsBeginImageContext(CGSize size);
第二种方法,argument:size大小 opaque透明度 scale缩放比例
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
参考如下:
Parameters
-
size
-
The size (measured in points) of the new bitmap context. This represents the size of the image returned by the
UIGraphicsGetImageFromCu
rrentImageContext function. To get the size of the bitmap in pixels, you must multiply the width and height values by the value in the scale parameter.
opaque -- 不透明(YES)
-
A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, you can specify
YES
for this parameter to optimize the bitmap storage. Specifying NO
means that the bitmap must include an alpha channel to handle any partially transparent pixels.
scale
-
The scale factor to apply to the bitmap. If you specify a value of
0.0
, the scale factor is set to the scale factor of the device’s main screen.