iOS 获取屏幕截图和缩放、裁剪图片

本文介绍了如何在iOS中使用UIGraphicsBeginImageContext方法获取屏幕截图和进行图片缩放。通过设置context的大小和利用renderInContext方法,可以捕获特定区域的屏幕图像。此外,通过调整drawInRect方法中的rect参数,可以实现图片的剪裁和缩放效果,例如改变rect的位置和大小以截取图片的不同部分。
摘要由CSDN通过智能技术生成
   1、截图:
-(UIImage *)screenImageWithSize:(CGSize )imgSize{
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate; //获取app的appdelegate,便于取到当前的window用来截屏
    [app.window.layer renderInContext:context];
    
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

UIGraphicsBeginImageContext()方法会构建一个基于位图(bitmap)的图形context,并且这个context会成为当前context。然后通过renderInContext方法把 app.window.layer 渲染进当前的图形context。最后通过UIGraphicsGetImageFromCurrentImageContext()获取当前图形context上的图片,从而获取屏幕截图。

  UIGraphicsBeginImageContext()方法传入唯一参

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 获取屏幕刷新率可以通过Core Animation框架中的CADisplayLink类实现。CADisplayLink是一个定时器类,可以监听屏幕的刷新频率,并在每一帧结束时发送回调。 首先需要创建一个CADisplayLink对象,并添加到主运行循环中: ``` CADisplayLink *displayLink = [UIScreen mainScreen].maximumFramesPerSecond == 0 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 0 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 60 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick60fps:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 120 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick120fps:)] : [UIScreen mainScreen].linkWithPreferredFramesPerSecond == 90 ? [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick90fps:)] : nil; [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; ``` 然后实现对应的回调方法,在每帧结束时获取屏幕的刷新率: ``` - (void)displayLinkTick:(CADisplayLink *)link { CGFloat frameInterval = link.duration/60.0; screenRefreshRate = (int)(1.0/frameInterval); } ``` 最后需要注意的是,在iOS 10之后,为了省电和减少GPU负担,屏幕刷新率可能会动态调整,因此获取到的屏幕刷新率并不一定是恒定的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值