iOS下截屏的几种方法

- (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 UIGraphicsGetImageFromCurrentImageContext 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.



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值