猎豆优选

UIImage+ST.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // UIImage+ST.m
  3. // STImageHead
  4. //
  5. // Created by 沈兆良 on 16/2/25.
  6. // Copyright © 2016年 沈兆良. All rights reserved.
  7. //
  8. #import "UIImage+ST.h"
  9. @implementation UIImage (ST)
  10. /**
  11. * 绘制图片圆角
  12. *
  13. * @param image <#image description#>
  14. * @param borderWidth <#borderWidth description#>
  15. * @param color <#color description#>
  16. *
  17. * @return <#return value description#>
  18. */
  19. + (UIImage *)imageWithClipImage:(UIImage *)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)color
  20. {
  21. // 图片的宽度和高度
  22. CGFloat imageWH = image.size.width;
  23. // 设置圆环的宽度
  24. CGFloat border = borderWidth;
  25. // 圆形的宽度和高度
  26. CGFloat ovalWH = imageWH + 2 * border;
  27. // 1.开启上下文
  28. UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalWH, ovalWH), NO, 0);
  29. // 2.画大圆
  30. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalWH, ovalWH)];
  31. [color set];
  32. [path fill];
  33. // 3.设置裁剪区域
  34. UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH, imageWH)];
  35. [clipPath addClip];
  36. // 4.绘制图片
  37. [image drawAtPoint:CGPointMake(border, border)];
  38. // 5.获取图片
  39. UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
  40. // 6.关闭上下文
  41. UIGraphicsEndImageContext();
  42. return clipImage;
  43. }
  44. /**
  45. * 指定区域图片的截图
  46. *
  47. * @param sourceImage 原始图片
  48. * @param clipRect 截取范围
  49. *
  50. * @return 截图图片
  51. */
  52. + (UIImage *)imageWithSourceImage:(UIImage *)sourceImage
  53. clipRect:(CGRect)clipRect
  54. {
  55. CGImageRef imageRef = sourceImage.CGImage;
  56. CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, clipRect);
  57. CGSize imageSize = clipRect.size;
  58. UIGraphicsBeginImageContext(imageSize);
  59. CGContextRef context = UIGraphicsGetCurrentContext();
  60. CGContextDrawImage(context, clipRect, subImageRef);
  61. UIImage *clipImage = [UIImage imageWithCGImage:subImageRef];
  62. UIGraphicsEndImageContext();
  63. return clipImage;
  64. }
  65. /**
  66. * 指定图片的比例
  67. *
  68. * @param sourceImage <#sourceImage description#>
  69. * @param scaleSize <#scaleSize description#>
  70. *
  71. * @return <#return value description#>
  72. */
  73. + (UIImage *)imageWithSourceImage:(UIImage *)sourceImage
  74. scaleSize:(CGSize)scaleSize
  75. {
  76. // 创建一个bitmap的context
  77. // 并把它设置成为当前正在使用的context
  78. UIGraphicsBeginImageContext(scaleSize);
  79. // 绘制改变大小的图片
  80. [sourceImage drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)];
  81. // 从当前context中创建一个改变大小后的图片
  82. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  83. // 使当前的context出堆栈
  84. UIGraphicsEndImageContext();
  85. // 返回新的改变大小后的图片
  86. return scaledImage;
  87. }
  88. /**
  89. * 截取指定位置的图片
  90. *
  91. * @param bounds <#bounds description#>
  92. *
  93. * @return <#return value description#>
  94. */
  95. - (UIImage *)croppedImage:(CGRect)bounds {
  96. CGFloat scale = MAX(self.scale, 1.0f);
  97. CGRect scaledBounds = CGRectMake(bounds.origin.x * scale+1, bounds.origin.y * scale+1, bounds.size.width * scale-2, bounds.size.height * scale-2);
  98. CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], scaledBounds);
  99. UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp];
  100. CGImageRelease(imageRef);
  101. return croppedImage;
  102. }
  103. @end