【问题标题】:UIImage size(width & Height) is decreasing after rotationUIImage 大小(宽度和高度)在旋转后减小
【发布时间】:2016-04-06 10:02:29
【问题描述】:

我正在根据比例值旋转 UIImage。我的 Scale 和 Rotation 运行良好,但问题是旋转 uiimage 后,图像大小(宽度和高度)正在减小。知道如何解决这个问题吗?
Image before Rotation

Image After Rotation

我正在使用的代码:

 UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.image.size.width, self.image.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(self.lastContentOffset/5);
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, 5.0);

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.image.size.width/2 , -self.image.size.height/2 , self.image.size.width, self.image.size.height), \[self.rotatedImage CGImage\]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.rotatedImage = newImage;

【问题讨论】:

    标签: ios objective-c uiimage


    【解决方案1】:

    假设您的图像大小为 HxH。旋转 45 度(M_PI/2 弧度)后,其大小将变为 sqrt(2)*H x sqrt(2)*H。所以你需要更大的上下文来适应旋转的图像。否则图片的边角会被切掉。此外,您将需要更大的UIImageView 以适应更大的图像。否则它将自动缩放到UIImageView 大小。

    【讨论】:

    • 谢谢伙计,我已经改变了上下文大小,它工作@s0urcer
    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多