天天省钱快报

UIView+YHExtend.m 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // UIView+YHExtend.m
  3. // YouHuiProject
  4. //
  5. // Created by jcymac on 2018/5/19.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "UIView+YHExtend.h"
  9. @implementation UIView (YHExtend)
  10. -(UIImage *)changeToImage{
  11. // UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0); //NO,YES 控制是否透明
  12. //
  13. // [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  14. //
  15. // UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  16. //
  17. // CGRect myImageRect = self.bounds;
  18. //
  19. // CGImageRef imageRef = image.CGImage;
  20. //
  21. // CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,myImageRect );
  22. //
  23. //
  24. //
  25. // CGContextRef context = UIGraphicsGetCurrentContext();
  26. //
  27. // CGContextDrawImage(context, myImageRect, subImageRef);
  28. //
  29. // UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
  30. //
  31. // CGImageRelease(subImageRef);
  32. //
  33. // UIGraphicsEndImageContext();
  34. // return smallImage;
  35. CGSize s = self.bounds.size;
  36. // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
  37. UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
  38. [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  39. UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
  40. UIGraphicsEndImageContext();
  41. return image;
  42. }
  43. @end