UIImage+Rotate.m 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // UIImage+Rotate.m
  3. // IphoneMapSdkDemo
  4. //
  5. // Created by wzy on 14-11-14.
  6. // Copyright (c) 2014年 Baidu. All rights reserved.
  7. //
  8. #import "UIImage+Rotate.h"
  9. @implementation UIImage (Rotate)
  10. - (UIImage*)imageRotatedByDegrees:(CGFloat)degrees
  11. {
  12. CGFloat width = CGImageGetWidth(self.CGImage);
  13. CGFloat height = CGImageGetHeight(self.CGImage);
  14. CGSize rotatedSize;
  15. rotatedSize.width = width;
  16. rotatedSize.height = height;
  17. UIGraphicsBeginImageContext(rotatedSize);
  18. CGContextRef bitmap = UIGraphicsGetCurrentContext();
  19. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
  20. CGContextRotateCTM(bitmap, degrees * M_PI / 180);
  21. CGContextRotateCTM(bitmap, M_PI);
  22. CGContextScaleCTM(bitmap, -1.0, 1.0);
  23. CGContextDrawImage(bitmap, CGRectMake(-rotatedSize.width/2, -rotatedSize.height/2, rotatedSize.width, rotatedSize.height), self.CGImage);
  24. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  25. UIGraphicsEndImageContext();
  26. return newImage;
  27. }
  28. @end