Rotate Newly Created iOS Image 90 Degrees Prior to Saving as PNG

后端 未结 1 1385
你的背包
你的背包 2020-12-29 17:35

I have read numerous answers related to this, but I still can\'t get it to work.

I have a view where a user can sign their name. Here\'s how it looks: http://d.pr/i/

相关标签:
1条回答
  • 2020-12-29 18:09

    I finally got this to work using one of the answers on this post: How to Rotate a UIImage 90 degrees?

    I used this method:

    - (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{
      //Calculate the size of the rotated view's containing box for our drawing space
      UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];
      CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
      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, (degrees * M_PI / 180));
    
      //Now, draw the rotated/scaled image into the context
      CGContextScaleCTM(bitmap, 1.0, -1.0);
      CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);
    
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
    }
    

    And then called it like this:

    //Rotate it
    UIImage *rotatedImage = [self imageRotatedByDegrees:signatureImage deg:90];
    

    Thanks, everyone.

    0 讨论(0)
提交回复
热议问题