zoukankan      html  css  js  c++  java
  • 完美解决scrollView 截屏图片模糊

    UIGraphicsBeginImageContext

     

    首先说明一下UIGraphicsBeginImageContextWithOptions 和UIGraphicsBeginImageContext区别:

     

    1: UIGraphicsBeginImageContext(CGSize size)

    参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。

    该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0

     

     

    2 UIGraphicsBeginImageContextWithOptions

     

    函数原型为:

    void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

    size——同UIGraphicsBeginImageContext

    opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。

    scale—–缩放因子

    特别说明,当缩放因子为0.0时候,图片不模糊

    //截屏

    - (UIImage *)CaptureScrollView:(UIScrollView *)scrollView{

        UIImage* image = nil;

    //    UIGraphicsBeginImageContext(scrollView.contentSize);  默认

        UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);

        {

            CGPoint savedContentOffset = scrollView.contentOffset;

            CGRect savedFrame = scrollView.frame;

            scrollView.contentOffset = CGPointZero;

            scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

            

            [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

            image = UIGraphicsGetImageFromCurrentImageContext();

            

            scrollView.contentOffset = savedContentOffset;

            scrollView.frame = savedFrame;

        }

        UIGraphicsEndImageContext();

        

        if (image != nil) {

            return image;

        }

        return nil;

    }

  • 相关阅读:
    LR--用栈实现移进--归约分析(demo)
    阿里云ECS服务器socket无法连接的问题
    select客户端模型封装——回调方式快速建立客户端
    select服务器端模型封装——回调方式快速建立服务端
    python实现的ocr接口
    汉字字典树
    linux下简易端口扫描器
    Linux下cs简单通讯(socket)
    POj 1321 棋盘问题 DFS 回溯
    HDU 1097 快速幂
  • 原文地址:https://www.cnblogs.com/haijieFeng/p/5568135.html
Copyright © 2011-2022 走看看