UIGraphicsBeginImageContextWithOptions使用及注意事项

来源:互联网 发布:线切割直线编程怎么编 编辑:程序博客网 时间:2024/05/01 04:58


     /*************************************************************************     *****************************绘图注意事项**********************************     *************************************************************************     *    UIGraphicsBeginImageContextWithOptions函数一定要有对应的     *    UIGraphicsEndImageContext函数作为结尾,不然会有内存泄漏     *    这个结尾函数可以多层嵌套:     *       UIGraphicsBeginImageContextWithOptions(......);     *       UIGraphicsBeginImageContextWithOptions(......);     *       UIGraphicsEndImageContext();     *       UIGraphicsEndImageContext();     */        UIGraphicsBeginImageContextWithOptions(vImg.frame.size, NO, 1.0);    CGContextRef context = UIGraphicsGetCurrentContext();    //    CGContextScaleCTM(context, -0.5, 1.0); // 缩放,为负时相当于翻转加缩放(不确定)//    CGContextTranslateCTM(context, -300, 100); // 偏移        /************************************************************************    *******************************绘制图片***********************************    *************************************************************************    *   会等比例缩放到全图显示,而且会翻转(原因是坐标系不同)    *    解决方法1:在绘制到context前通过矩阵垂直翻转坐标系    *        CGContextTranslateCTM(context, 0, height);    *        CGContextScaleCTM(context, 1.0, -1.0);    *    解决方法2:使用drawInRect方法(使用这个方法,绘制后和以前图片的宽高比例相同)    *    解决方法3:颠倒画布    *        glMatrixMode(GL_PROJECTION);    *        glLoadIdentity();    *        glOrthof( 0, framebufferWidth, framebufferHeight, 0, -1, 1 );    *************************************************************************/    CGContextTranslateCTM(context, 0, vImg.frame.size.height);    CGContextScaleCTM(context, 1.0, -1.0);    CGContextDrawImage(context, vImg.bounds, img.CGImage); // 绘制图片,也可通过drawInRect方法        UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();


0 0