使用Core Graphics对中心,调整大小和旋转图像

时间:2014-01-06 19:35:49

标签: ios core-graphics

我想将图像居中并调整大小以适应核心图形。 现在我设法调整大小并旋转它以便正确显示,但它没有居中。 我做错了什么?

    CGContextSaveGState(context);
    UIImage * image = self.backgroundImage;
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

    CGRect cropBox = CGRectMake(0, 0, image.size.width, image.size.height);
    CGRect targetRect = self.bounds;

    CGFloat xScale = targetRect.size.width / cropBox.size.width;
    CGFloat yScale = targetRect.size.height / cropBox.size.height;
    CGFloat scaleToApply = xScale < yScale ? xScale : yScale;

    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextConcatCTM(context, CGAffineTransformMakeScale(scaleToApply, scaleToApply));

    //center - width * scaleToApply/2.0
    float x = CGRectGetMidX(self.bounds) -  (image.size.width * scaleToApply)/2.0;
    NSLog(@"%f %f",self.center.x, CGRectGetMidX(self.bounds));
    imageRect =  CGRectOffset(imageRect, x, 0);

    CGContextDrawImage(context, imageRect, image.CGImage);

    CGContextStrokePath(context);
    CGContextRestoreGState(context);

Not centered!

0 个答案:

没有答案