【问题标题】:Rotating UIImageView data -旋转 UIImageView 数据 -
【发布时间】:2014-04-11 06:13:09
【问题描述】:

您好,在我的 iOS 应用程序中,我想将 UIImage 数据旋转 90 度。

我使用下面的代码进行 180 度旋转 - 效果很好。

- (UIImage*)upsideDownBunny:(UIImage *)img {

    CGSize imgSize = [img size];
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context,  M_PI);
    CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
    [img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

但是当我尝试旋转 90 度时它不起作用 - 请纠正我

- (UIImage *)upsideDownBunny:(UIImage *)img {

    CGSize imgSize = [img size];
    UIGraphicsBeginImageContext(imgSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context,  M_PI_2);
    CGContextTranslateCTM(context, -imgSize.width, -imgSize.height);
    [img drawInRect:CGRectMake(0, 0, imgSize.width, imgSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

【问题讨论】:

    标签: ios objective-c uiimageview rotation


    【解决方案1】:

    试试这个...

    UIImage * LandscapeImage = [UIImage imageNamed: imgname];
    UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                     scale: 1.0
                                               orientation: UIImageOrientationLeft];
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      试试这个

      - (UIImage *)upsideDownBunny:(UIImage *)image {
      
          // Rotate in degrees
          CGFloat degrees = M_PI_2;
      
          // Calculate the size of the rotated view's containing box for our drawing space
          UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
          CGAffineTransform transform = CGAffineTransformMakeRotation(degrees);
          rotatedViewBox.transform = transform;
          CGSize rotatedSize = rotatedViewBox.frame.size;
      
          // Create image context
          UIGraphicsBeginImageContext(rotatedSize);
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          // Move the origin to the middle of the image.
          CGContextTranslateCTM(context, rotatedSize.width * 0.5f, rotatedSize.height * 0.5f);
      
          // Rotate the image context
          CGContextRotateCTM(context, degrees);
      
          // Now, draw the rotated/scaled image into the context
          CGContextScaleCTM(context, 1.0, -1.0);
          CGContextDrawImage(context, CGRectMake(-image.size.width * 0.5f, -image.size.height * 0.5f, image.size.width, image.size.height), [image CGImage]);
      
          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return newImage;
      }
      

      问题是您的上下文围绕左上角 (0,0) 旋转,旋转 90 度后它将超出范围。所以你需要把上下文的原点移到中间。

      【讨论】:

      • 这是可行的,但图像的高度会缩小。你能告诉我这个问题吗?
      • @SplatterStrikes 新的会发生什么?只需点击已编辑...你会得到旧的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      相关资源
      最近更新 更多