如何从文件中旋转和裁剪 iOS CGImage

How to rotate and crop iOS CGImage from file

我在 iOS 中遇到图像旋转问题。我正在应用程序的后台执行一些图像处理...我想旋转和裁剪图像。目前,旋转似乎工作正常,但无论我尝试了什么,裁剪都关闭了。我在 GIMP 中执行了这些相同的操作,发现图像裁剪正确,所以我相信它与 Quartz 坐标系有关。此外,任一方向的弧度越大,作物变得越 "off" 。这是我用来旋转和裁剪的代码:

+(UIImage*)imageWithImageFile:(NSString*)imageFile
                      radians:(float)radians
                    imageSize:(CGSize)imageSize
                croppedToRect:(CGRect)croppedToRect
{
    UIImage* returnImg = nil;
    @autoreleasepool {
        CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
        CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);
        UIGraphicsBeginImageContext(croppedToRect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetShouldAntialias(context, YES);
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

        CGContextTranslateCTM(context, imageSize.width * 0.5,
                              imageSize.height * 0.5);
        CGContextRotateCTM(context, radians);
        CGContextTranslateCTM(context, -imageSize.width * 0.5, -imageSize.height * 0.5);

        //Translate and scale upside-down to compensate for Quartz's inverted coordinate system
        CGContextTranslateCTM(context, 0.0, imageSize.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawImage(context, (CGRect) {croppedToRect.origin, imageSize}, image);
        returnImg = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
        CGDataProviderRelease(dataProvider);
        CGImageRelease(image);
    }

    return returnImg;
}

我相信这是解决方案:由于上下文大小不同并且在原始图像中具有不同的原点,因此您必须偏移 "paint" 图像在上下文中的点(canvas 给你们这些更有视觉感的人)。您首先找到居中上下文的原点,然后将其与所需作物的原点进行比较。可以从上下文的中心点减去差异(或偏移),这将偏移图像绘制的点。

    CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);
    CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, YES, kCGRenderingIntentDefault);

    UIGraphicsBeginImageContext(croppedToRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPoint contextCenter = CGPointMake(croppedToRect.size.width * 0.5f,
                                          croppedToRect.size.height * 0.5f);
    CGPoint centerOfImage = CGPointMake(imageSize.width * 0.5f,
                                        imageSize.height * 0.5f);
    CGPoint contextOriginInImage = CGPointMake(centerOfImage.x - contextCenter.x,
                                        centerOfImage.y - contextCenter.y);
    CGPoint desiredOrigin = croppedToRect.origin;
    CGPoint offset = CGPointMake(desiredOrigin.x - contextOriginInImage.x,
                                 desiredOrigin.y - contextOriginInImage.y);
    CGContextTranslateCTM(context, contextCenter.x - offset.x, contextCenter.y - offset.y);
    CGContextRotateCTM(context, radians);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    CGContextDrawImage(context, CGRectMake(-imageSize.width * 0.5f,
                                          -imageSize.height * 0.5f,
                                          imageSize.width,
                                          imageSize.height), image);
    returnImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGDataProviderRelease(dataProvider);
    CGImageRelease(image);