iphone - 使用 UIImage drawInRect 仍然将其呈现为上下颠倒

标签 iphone uiimage cgcontext

我在这个网站上读过一些关于使用drawInRect而不是CGContext绘制方法的帖子。它仍然把它画得颠倒吗?我认为使用 drawInRect 会将其正面朝上打印,坐标系起始于左上角?

-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
[image drawInRect:CGRectMake(x, size.height-y-height, width, height)];
UIGraphicsPopContext();
}

请注意,我正在执行 size.height-y-height ,如果我不这样做,它就不会达到我预期的效果(假设 drawInRect 使用左上角坐标系)。它使用上面的代码呈现在正确的位置,但仍然颠倒。救命!!!!!!

更新

感谢下面的回答,这是工作方法

-(void) drawImage:(UIImage*) image atX:(float) x andY:(float) y withWidth:(float) width andHeight:(float) height onContext:(CGContextRef) context{
UIGraphicsPushContext(context);
CGContextSaveGState(context); 
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[image drawInRect:CGRectMake(x, y, width, height)];
CGContextRestoreGState(context);
UIGraphicsPopContext();
}

最佳答案

    UIGraphicsBeginImageContext(imageViewSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();

// Draw the image in the upper left corner (0,0) with its actual size
CGContextDrawImage(imageContext, imageViewRect, oldImage.CGImage);

//  As it draws the image from lower right corner, 
//  following code will flip it up side down vertically.


CGContextTranslateCTM(imageContext, 0.0, 0.0);
CGContextScaleCTM(imageContext, 1.0, -1.0);

CGContextDrawImage(imageContext, imageViewRect, oldImage.CGImage);

关于iphone - 使用 UIImage drawInRect 仍然将其呈现为上下颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4496191/

相关文章:

iphone - 将在 Interface Builder 中创建的 UITabBarController 设置为委托(delegate)

ios - 我怎样才能比CGContext描边路径更快地渲染线?

iphone - 有时 UIImageView 似乎拒绝使用 iPhone 相机拍摄的图像

ios - iOS 中的 Google map 不显示多个图钉

iphone - 将 UIImage/CGImage 渲染到 CGPDFContext 会导致...空白!

iphone - 单个 UIImage 多个 UIImageView?

ios - 在 CGContext 中定位预制的 CGPath

ios - CGContextAddPath 控制台错误?

iphone - 如何在 UITableView Cell 中制作透明的 UIButton

ios - Swift - 自定义单元格内容(UILabel、UIImageView)