UIImage+ClipperExtends.m 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // UIImage+ClipperExtends.m
  3. // HKClipperDemo
  4. //
  5. // Created by hukaiyin on 16/8/30.
  6. // Copyright © 2016年 hukaiyin. All rights reserved.
  7. //
  8. #import "UIImage+ClipperExtends.h"
  9. @implementation UIImage (ClipperExtends)
  10. -(UIImage*)scaledToSize:(CGSize)newSize withScale:(BOOL)withScale {
  11. CGFloat scale = 1;
  12. if (withScale) {
  13. scale = [UIScreen mainScreen].scale;
  14. }
  15. newSize = (CGSize){newSize.width * scale, newSize.height * scale};
  16. // Create a graphics image context
  17. UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
  18. // UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
  19. // Tell the old image to draw in this new context, with the desired
  20. // new size
  21. [self drawInRect:(CGRect){CGPointZero, newSize}];
  22. // Get the new image from the context
  23. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  24. // End the context
  25. UIGraphicsEndImageContext();
  26. // Return the new image.
  27. return newImage;
  28. }
  29. @end