美文网首页
用户空间和设备空间转换

用户空间和设备空间转换

作者: lltree | 来源:发表于2016-09-05 19:08 被阅读73次

    CorePlot使用的用户空间和设备空间相互转换:
    用户空间:用户空间指的是在一个View或者Layer上按照“点”显示的
    设备空间:设备空间指的是在一个View或者Layer上按照“像素”显示的

    /// @name Quartz Pixel-Alignment Functions
    /// @{
    //用户空间和设备空间的坐标或者区域转换
    /**
     *  @brief A function called to align a point in a CGContext.
     **/
    typedef CGPoint (*CPTAlignPointFunction)(CGContextRef, CGPoint);//函数指针
    
    /**
     *  @brief A function called to align a rectangle in a CGContext.
     **/
    typedef CGRect (*CPTAlignRectFunction)(CGContextRef, CGRect);
    
    CGPoint CPTAlignPointToUserSpace(CGContextRef context, CGPoint point);
    CGSize CPTAlignSizeToUserSpace(CGContextRef context, CGSize size);
    CGRect CPTAlignRectToUserSpace(CGContextRef context, CGRect rect);
    
    CGPoint CPTAlignIntegralPointToUserSpace(CGContextRef context, CGPoint point);
    CGRect CPTAlignIntegralRectToUserSpace(CGContextRef context, CGRect rect);
    
    CGRect CPTAlignBorderedRectToUserSpace(CGContextRef context, CGRect rect, CPTLineStyle *borderLineStyle);
    
    /// @}
    

    代码示例:

    #pragma mark -
    #pragma mark Integral Geometry Conversions
    
    /**
     *  @brief Aligns a point in user space between integral coordinates in device space.
     *
     *  Ensures that the x and y coordinates are between pixels in device space.
     *
     *  @param context The graphics context.
     *  @param point The point in user space.
     *  @return The device aligned point in user space.
     **/
    /*
     (lldb) po point
     执行前
     (x = -12, y = 109.19999999999999)
     (x = -12, y = 109.19999999999999)
     执行后
     (lldb) po point
     (x = -12, y = 109)
     (x = -12, y = 109)
     */
    //确保设备的x和y坐标像素之间的空间
    CGPoint CPTAlignIntegralPointToUserSpace(CGContextRef context, CGPoint point)
    {
        //由用户空间的点 转换到设备空间像素要 乘以3倍数(分辨率)
        point = CGContextConvertPointToDeviceSpace(context, point);
    
        //然后再对像素进行处理
        point.x = round(point.x);//如果是小数则四舍五入
        point.y = ceil( point.y - CPTFloat(0.5) );//如果参数是小数,则求最小的整数但不小于本身
    
        //像素值转换成用户空间上的点
        return CGContextConvertPointToUserSpace(context, point);
    }
    

    相关文章

      网友评论

          本文标题:用户空间和设备空间转换

          本文链接:https://www.haomeiwen.com/subject/zmgrettx.html