Posted By

oppey on 05/23/11


Tagged


Versions (?)

UIImageの回転


 / Published in: Objective C
 

  1. - (UIImage *) rotateImage:(UIImage *)img angle:(int)angle
  2. {
  3. CGImageRef imgRef = [img CGImage];
  4. CGContextRef context;
  5.  
  6. switch (angle) {
  7. case 90:
  8. UIGraphicsBeginImageContext(CGSizeMake(img.size.height, img.size.width));
  9. context = UIGraphicsGetCurrentContext();
  10. CGContextTranslateCTM(context, img.size.height, img.size.width);
  11. CGContextScaleCTM(context, 1.0, -1.0);
  12. CGContextRotateCTM(context, M_PI/2.0);
  13. break;
  14. case 180:
  15. UIGraphicsBeginImageContext(CGSizeMake(img.size.width, img.size.height));
  16. context = UIGraphicsGetCurrentContext();
  17. CGContextTranslateCTM(context, img.size.width, 0);
  18. CGContextScaleCTM(context, 1.0, -1.0);
  19. CGContextRotateCTM(context, -M_PI);
  20. break;
  21. case 270:
  22. UIGraphicsBeginImageContext(CGSizeMake(img.size.height, img.size.width));
  23. context = UIGraphicsGetCurrentContext();
  24. CGContextScaleCTM(context, 1.0, -1.0);
  25. CGContextRotateCTM(context, -M_PI/2.0);
  26. break;
  27. default:
  28. NSLog(@"you can select an angle of 90, 180, 270");
  29. return nil;
  30. }
  31.  
  32. CGContextDrawImage(context, CGRectMake(0, 0, img.size.width, img.size.height), imgRef);
  33. UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
  34.  
  35. UIGraphicsEndImageContext();
  36. return ret;
  37. }
  38.  

Report this snippet  

You need to login to post a comment.