How to Rotate a UIImage 90 degrees?

前端 未结 19 1360
天涯浪人
天涯浪人 2020-11-22 08:45

I have a UIImage that is UIImageOrientationUp (portrait) that I would like to rotate counter-clockwise by 90 degrees (to landscape). I don\'t want

19条回答
  •  再見小時候
    2020-11-22 08:54

    Rotate Image by 90 degree (clockwise/anti-clockwise direction)

    Function call -

     UIImage *rotatedImage = [self rotateImage:originalImage clockwise:YES];
    

    Implementation:

    - (UIImage*)rotateImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise
      {
        CGSize size = sourceImage.size;
        UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
        [[UIImage imageWithCGImage:[sourceImage CGImage]
                             scale:1.0
                       orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft]
                       drawInRect:CGRectMake(0,0,size.height ,size.width)];
    
       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
       UIGraphicsEndImageContext();
    
       return newImage;
      }
    

提交回复
热议问题