CGContext上的垂直翻转(Vertical flip of CGContext)

2019-06-26 01:55发布

我有我试图呈现到使用一个UIImage一个UIView [CALayer的renderInContext:]。 然而,我发现,得到的图像垂直翻转。 我有点希望这由于不同的坐标系。 然而,我再试着和仿射翻转背景下恢复正常转换 - 但它不会有任何影响:

CGAffineTransform flipVertical = CGAffineTransformMake(
    1, 0, 0, -1, 0, imageContextHeight
);
CGContextConcatCTM(imageContext, flipVertical);
CGImageRef cgImage = CGBitmapContextCreateImage(imageContext);
UIImage* uiImage = [[UIImage imageWithCGImage:cgImage] retain];
CGImageRelease(cgImage);

我究竟做错了什么?

Answer 1:

在CTM影响未来绘图; 你捕获您已经绘制的内容。 您需要在绘制之前,而不是之后到Concat的这种转变。



Answer 2:

下面是一些代码,我写在此基础上的答案,如果它是有帮助的人:

#import <QuartzCore/QuartzCore.h>
...
+ (UIImage *) flipImageVertically:(UIImage *)originalImage {
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];

    UIGraphicsBeginImageContext(tempImageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform flipVertical = CGAffineTransformMake(
            1, 0, 0, -1, 0, tempImageView.frame.size.height
    );
    CGContextConcatCTM(context, flipVertical);  

    [tempImageView.layer renderInContext:context];

    UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [tempImageView release];

    return flipedImage;
}   


Answer 3:

下面是相同的代码上面(benvolioT的答案),但在SWIFT 4.0

import QuartzCore
...
func flipImageVertically(originalImage:UIImage) -> UIImage{

    let tempImageView:UIImageView = UIImageView(image: originalImage)
    UIGraphicsBeginImageContext(tempImageView.frame.size)
    let context:CGContext = UIGraphicsGetCurrentContext()!
    let flipVertical: CGAffineTransform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: tempImageView.frame.size.height)

    context.concatenate(flipVertical)
    tempImageView.layer.render(in: context)

    let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return flippedImage
}

这里是SWIFT更好的方法:

func flipImageVertically(originalImage:UIImage) -> UIImage {
    let image:UIImage = UIImage(CGImage: originalImage.CGImage, scale: 1.0, orientation: UIImageOrientation.RightMirrored)!
    return image
}


Answer 4:

我迅速使用此代码:

            UIGraphicsBeginImageContextWithOptions(myImageView.image!.size, false, 1.0)

            var context = UIGraphicsGetCurrentContext()

            //move and invert canvas by scaling
            CGContextTranslateCTM(context, myImageView.image!.size.width, 0)
            CGContextScaleCTM(context, -1, 1)

            myImageView.image!.drawInRect(CGRect(x: 0, y: 0, width: myImageView.image!.size.width, height: myImageView.image!.size.height))

            // move back and reinvert
            CGContextScaleCTM(context, 1, 1)
            CGContextTranslateCTM(context, -myImageView.image!.size.width, 0)

            let flippedImg = UIGraphicsGetImageFromCurrentImageContext()

            myImageView.image = flippedImg

            UIGraphicsEndImageContext()


Answer 5:

斯威夫特4:

let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: originalImage.size.height)
ctx.concatenate(flipVertical)


Answer 6:

在斯威夫特4使用此功能:

func drawImage(image: UIImage) -> UIImage {
    // Setup our context
    let opaque = false
    let scale: CGFloat = 0 // 0 means we use scale factor of the device’s main screen.
    UIGraphicsBeginImageContextWithOptions(image.size, opaque, scale)
    let context = UIGraphicsGetCurrentContext()!
    let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: image.size.height)
    context.concatenate(flipVertical) // flip by y

    // draw your another figures
    //...
    // Drawing complete, set context to image and finish
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}


文章来源: Vertical flip of CGContext