使用CGContextSetFillColorWithColor后,为什么我的图像会颠倒

mat*_*ace 2 core-graphics objective-c mkannotation ios

我正在尝试将颜色填充应用于MKAnnotation.我发现一些代码几乎可以工作,但由于某种原因填充的图像在应用填充后颠倒了.

这是我在地图引脚上运行的当前代码.

CGRect rect = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
    UIGraphicsBeginImageContext(self.image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToMask(context, rect, self.image.CGImage);
    CGContextSetFillColorWithColor(context, [[UIColor grayColor] CGColor]);
    CGContextFillRect(context, rect);
    CGContextRotateCTM(context, 90);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImage *flippedImage = [UIImage imageWithCGImage:img.CGImage
                                                scale:1.0 orientation:self.image.imageOrientation];

    self.image = flippedImage;
Run Code Online (Sandbox Code Playgroud)

以下是此代码运行后引脚的外观.

http://d.pr/i/UaPU

我在想,如果我将当前的图像方向应用于flippedImage,那将会起作用,但这不起作用.我也尝试完全设置self.image = img;和删除flippedImage var,但结果仍然相同.

小智 6

关于UIView坐标系,CGContext坐标系垂直翻转.只需将其翻转为:CGContextTranslateCTM(ctx,0,imageHeight);, CGContextScaleCTM(ctx,1,-1);