UIImage+LookinServer.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // UIImage+LookinServer.m
  3. // LookinServer
  4. //
  5. // Created by Li Kai on 2019/5/14.
  6. // https://lookin.work
  7. //
  8. #import "UIImage+LookinServer.h"
  9. #import "Objc/runtime.h"
  10. #import "LookinServerDefines.h"
  11. @implementation UIImage (LookinServer)
  12. + (void)load {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. Method oriMethod = class_getClassMethod([self class], @selector(imageNamed:));
  16. Method newMethod = class_getClassMethod([self class], @selector(lks_imageNamed:));
  17. method_exchangeImplementations(oriMethod, newMethod);
  18. oriMethod = class_getClassMethod([self class], @selector(imageWithContentsOfFile:));
  19. newMethod = class_getClassMethod([self class], @selector(lks_imageWithContentsOfFile:));
  20. method_exchangeImplementations(oriMethod, newMethod);
  21. });
  22. }
  23. + (UIImage *)lks_imageNamed:(NSString *)name {
  24. UIImage *image = [self lks_imageNamed:name];
  25. image.lks_imageSourceName = name;
  26. return image;
  27. }
  28. + (UIImage *)lks_imageWithContentsOfFile:(NSString *)path {
  29. UIImage *image = [self lks_imageWithContentsOfFile:path];
  30. NSString *fileName = [[path componentsSeparatedByString:@"/"].lastObject componentsSeparatedByString:@"."].firstObject;
  31. image.lks_imageSourceName = fileName;
  32. return image;
  33. }
  34. - (void)setLks_imageSourceName:(NSString *)lks_imageSourceName {
  35. [self lookin_bindObject:lks_imageSourceName.copy forKey:@"lks_imageSourceName"];
  36. }
  37. - (NSString *)lks_imageSourceName {
  38. return [self lookin_getBindObjectForKey:@"lks_imageSourceName"];
  39. }
  40. - (NSData *)lookin_data {
  41. return UIImagePNGRepresentation(self);
  42. }
  43. @end