代码之家  ›  专栏  ›  技术社区  ›  airportyh

保存到磁盘时翻转图像

  •  0
  • airportyh  · 技术社区  · 14 年前

    我目前正在使用以下代码将cgContext写入磁盘上的png:

    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage *myImage = [UIImage imageWithCGImage:image];
    NSData *data = UIImagePNGRepresentation(myImage);
    [data writeToFile:path atomically:YES];
    

    我发现结果文件的方向与它在屏幕上(iPhone上)显示的方向相反。保存时翻转此图像的最佳方法是什么,以便稍后加载图像时,它将正确向上?

    1 回复  |  直到 14 年前
        1
  •  3
  •   Ben Zotto sberry    14 年前

    uiimage和cg上下文的坐标系是相互翻转的。在绘制CGContext之前,可以通过执行以下操作来翻转它:

    CGContextTranslateCTM(context, 0, height);
    CGContextScaleCTM(context, 1.0, -1.0);