猎豆优选

UIImage+Crop.m 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // UIImage+Crop.m
  3. // CropImage
  4. //
  5. // Created by limuyun on 2017/1/11.
  6. // Copyright © 2017年 biiway. All rights reserved.
  7. //
  8. #import "UIImage+Crop.h"
  9. @implementation UIImage (Crop)
  10. - (UIImage *)resizeImageWithSize:(CGSize)newSize {
  11. CGFloat newWidth = newSize.width;
  12. CGFloat newHeight = newSize.height;
  13. float width = self.size.width;
  14. float height = self.size.height;
  15. if (width != newWidth || height != newHeight) {
  16. UIGraphicsBeginImageContextWithOptions(CGSizeMake(newWidth, newHeight), YES, [UIScreen mainScreen].scale);
  17. [self drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
  18. UIImage *resized = UIGraphicsGetImageFromCurrentImageContext();
  19. UIGraphicsEndImageContext();
  20. return resized;
  21. }
  22. return self;
  23. }
  24. - (UIImage *)ovalClip {
  25. CGSize size = self.size;
  26. UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
  27. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
  28. [path addClip];
  29. [self drawAtPoint:CGPointZero];
  30. UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
  31. UIGraphicsEndImageContext();
  32. return image;
  33. }
  34. @end