ImageUtils.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // ImageUtils.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/20/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ImageUtils.h"
  9. #import "AppDelegate.h"
  10. @implementation ImageUtils
  11. + (CGRect)scaleToSize:(CGRect )from to:(CGSize)to
  12. {
  13. if(from.size.width/from.size.height>to.width/to.height)
  14. {
  15. return CGRectMake(from.origin.x, from.origin.y, to.width, to.width*from.size.height/from.size.width);
  16. }
  17. else
  18. {
  19. return CGRectMake(from.origin.x, from.origin.y, to.height*from.size.width/from.size.height, to.height);
  20. }
  21. // // 创建一个bitmap的context
  22. // // 并把它设置成为当前正在使用的context
  23. // UIGraphicsBeginImageContext(size);
  24. // // 绘制改变大小的图片
  25. // [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  26. // // 从当前context中创建一个改变大小后的图片
  27. // UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  28. // // 使当前的context出堆栈
  29. // UIGraphicsEndImageContext();
  30. // // 返回新的改变大小后的图片
  31. //
  32. // // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  33. // return scaledImage;
  34. }
  35. + (CGRect)rectAlign:(CGRect )parent rect:(CGRect)rect hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
  36. {
  37. // double cx=parent.origin.x+parent.size.width/2;
  38. // double cy=parent.origin.y+parent.size.height/2;
  39. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  40. if([hAlign.lowercaseString isEqualToString:@"center"])
  41. {
  42. rect=CGRectMake(centerpoint.x-rect.size.width/2, rect.origin.y, rect.size.width, rect.size.height);
  43. }
  44. else
  45. if([hAlign.lowercaseString isEqualToString:@"left"])
  46. {
  47. rect=CGRectMake(parent.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  48. }
  49. else
  50. if([hAlign.lowercaseString isEqualToString:@"right"])
  51. {
  52. rect=CGRectMake(parent.origin.x+parent.size.width-rect.size.width, rect.origin.y, rect.size.width, rect.size.height);
  53. }
  54. if([vAlign.lowercaseString isEqualToString:@"middle"])
  55. {
  56. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  57. }
  58. return rect;
  59. }
  60. + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
  61. {
  62. if(path.length==0)
  63. return nil;
  64. path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  65. path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  66. // path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
  67. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  68. if(appDelegate.bEnable_Cache==false)
  69. return nil;
  70. NSData* data = nil;
  71. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  72. NSString *cachefolder = [paths objectAtIndex:0];
  73. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  74. NSString *filePath = [img_cache stringByAppendingPathComponent:path];
  75. NSFileManager* fileManager = [NSFileManager defaultManager];
  76. if( [fileManager fileExistsAtPath:filePath ])
  77. {
  78. data = [NSData dataWithContentsOfFile: filePath];
  79. }
  80. return data;
  81. }
  82. + (NSData*) load_img:(NSString*) filename
  83. {
  84. if(filename.length==0)
  85. return nil;
  86. NSData* data = nil;
  87. //
  88. //
  89. // NSString* tempDir = NSTemporaryDirectory();
  90. // NSString *filePath = [tempDir stringByAppendingPathComponent:filename];
  91. //
  92. NSFileManager* fileManager = [NSFileManager defaultManager];
  93. if( [fileManager fileExistsAtPath:filename ])
  94. {
  95. data = [NSData dataWithContentsOfFile: filename];
  96. }
  97. return data;
  98. }
  99. @end