ImageUtils.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. +(UIImage*)img_compress:(UIImage*)image kbsize:(float) size
  12. {
  13. //UIImage *image=[UIImage imageNamed:@"xxoo.jpeg"];
  14. NSData *imageData=UIImageJPEGRepresentation(image, 1.f);
  15. if(size>imageData.length/1024)
  16. return image;
  17. // CGFloat size=40.f;// kb
  18. CGFloat scale=size/(imageData.length/1024);
  19. scale = sqrt (scale);
  20. CGSize newsize=image.size;
  21. newsize.height = newsize.height*scale;
  22. newsize.width = newsize.width*scale;
  23. return [self scaleImageToSize:image size:newsize];
  24. // NSData *newData=UIImageJPEGRepresentation(image, scale);
  25. // UIImage* ret= [[UIImage alloc] initWithData:newData];
  26. //
  27. // return ret;
  28. }
  29. + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path size:(CGSize)size allow_enlarge:(bool)allow_enlarge
  30. {
  31. NSData* data=[self load_cached_img:filename loadFrom:path];
  32. return data;
  33. UIImage* img = [UIImage imageWithData:data];
  34. if(!allow_enlarge&&(size.width>img.size.width|| size.height>img.size.height))
  35. return data;
  36. // CGRect rect = [self scaleToSize:CGRectMake(0, 0, img.size.width, img.size.height) to:size];
  37. UIImage* scaledimg=[self scaleImageToSize:img size:size];
  38. NSData *scaledData = UIImagePNGRepresentation(scaledimg);
  39. return scaledData;
  40. }
  41. + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
  42. {
  43. if(path.length==0)
  44. return nil;
  45. path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
  46. path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
  47. // path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
  48. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  49. if(appDelegate.bEnable_Cache==false)
  50. return nil;
  51. NSData* data = nil;
  52. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  53. NSString *cachefolder = [paths objectAtIndex:0];
  54. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  55. NSString *filePath = [img_cache stringByAppendingPathComponent:path];
  56. NSFileManager* fileManager = [NSFileManager defaultManager];
  57. if( [fileManager fileExistsAtPath:filePath ])
  58. {
  59. data = [NSData dataWithContentsOfFile: filePath];
  60. }
  61. return data;
  62. }
  63. + (CGRect)scaleToSize:(CGRect )from to:(CGSize)to
  64. {
  65. if(from.size.width/from.size.height>to.width/to.height)
  66. {
  67. return CGRectMake(from.origin.x, from.origin.y, to.width, to.width*from.size.height/from.size.width);
  68. }
  69. else
  70. {
  71. return CGRectMake(from.origin.x, from.origin.y, to.height*from.size.width/from.size.height, to.height);
  72. }
  73. // // 创建一个bitmap的context
  74. // // 并把它设置成为当前正在使用的context
  75. // UIGraphicsBeginImageContext(size);
  76. // // 绘制改变大小的图片
  77. // [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  78. // // 从当前context中创建一个改变大小后的图片
  79. // UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  80. // // 使当前的context出堆栈
  81. // UIGraphicsEndImageContext();
  82. // // 返回新的改变大小后的图片
  83. //
  84. // // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  85. // return scaledImage;
  86. }
  87. + (UIImage *)scaleImageToSize:(UIImage *)img size:(CGSize)size{
  88. // 创建一个bitmap的context
  89. // 并把它设置成为当前正在使用的context
  90. UIGraphicsBeginImageContext(size);
  91. // 绘制改变大小的图片
  92. [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  93. // 从当前context中创建一个改变大小后的图片
  94. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  95. // 使当前的context出堆栈
  96. UIGraphicsEndImageContext();
  97. // 返回新的改变大小后的图片
  98. // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  99. return scaledImage;
  100. }
  101. + (CGRect)rectAlign:(CGRect )parent rect:(CGRect)rect hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
  102. {
  103. // double cx=parent.origin.x+parent.size.width/2;
  104. // double cy=parent.origin.y+parent.size.height/2;
  105. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  106. if([hAlign.lowercaseString isEqualToString:@"center"])
  107. {
  108. rect=CGRectMake(centerpoint.x-rect.size.width/2, rect.origin.y, rect.size.width, rect.size.height);
  109. }
  110. else
  111. if([hAlign.lowercaseString isEqualToString:@"left"])
  112. {
  113. rect=CGRectMake(parent.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  114. }
  115. else
  116. if([hAlign.lowercaseString isEqualToString:@"right"])
  117. {
  118. rect=CGRectMake(parent.origin.x+parent.size.width-rect.size.width, rect.origin.y, rect.size.width, rect.size.height);
  119. }
  120. if([vAlign.lowercaseString isEqualToString:@"middle"])
  121. {
  122. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  123. }
  124. return rect;
  125. }
  126. + (NSData*) load_img:(NSString*) filename
  127. {
  128. if(filename.length==0)
  129. return nil;
  130. NSData* data = nil;
  131. //
  132. //
  133. // NSString* tempDir = NSTemporaryDirectory();
  134. // NSString *filePath = [tempDir stringByAppendingPathComponent:filename];
  135. //
  136. NSFileManager* fileManager = [NSFileManager defaultManager];
  137. if( [fileManager fileExistsAtPath:filename ])
  138. {
  139. data = [NSData dataWithContentsOfFile: filename];
  140. }
  141. return data;
  142. }
  143. +(UIImage*)generateBarCode:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
  144. {
  145. //return nil;
  146. // 生成二维码图片
  147. CIImage *barcodeImage;
  148. NSData *data = [barCodeStr dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
  149. CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
  150. [filter setValue:data forKey:@"inputMessage"];
  151. barcodeImage = [filter outputImage];
  152. // 消除模糊
  153. CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame
  154. CGFloat scaleY = height / barcodeImage.extent.size.height;
  155. CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
  156. CIContext *context = [CIContext contextWithOptions:nil];
  157. CGImageRef cgImage = [context createCGImage:transformedImage fromRect:[transformedImage extent]];
  158. UIImage* ret = [UIImage imageWithCGImage:cgImage];
  159. CGImageRelease(cgImage);
  160. return ret;
  161. }
  162. +(NSString*)generateBarCodeFile:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
  163. {
  164. UIImage* img = [self generateBarCode:barCodeStr width:width height:height];
  165. NSString* qrpath=nil;
  166. NSString* temp = NSTemporaryDirectory();
  167. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  168. qrpath=[temp stringByAppendingPathComponent:filename];
  169. [UIImagePNGRepresentation(img)writeToFile: qrpath atomically:YES];
  170. return qrpath;
  171. }
  172. +(NSString*)generateQRCodeFile:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
  173. {
  174. UIImage* img = [self generateQRCode:barCodeStr width:width height:height];
  175. NSString* qrpath=nil;
  176. NSString* temp = NSTemporaryDirectory();
  177. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  178. qrpath=[temp stringByAppendingPathComponent:filename];
  179. [UIImagePNGRepresentation(img)writeToFile: qrpath atomically:YES];
  180. return qrpath;
  181. }
  182. +(UIImage*)generateQRCode:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
  183. {
  184. {
  185. // 生成二维码图片
  186. CIImage *barcodeImage;
  187. NSData *data = [barCodeStr dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
  188. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  189. [filter setValue:data forKey:@"inputMessage"];
  190. barcodeImage = [filter outputImage];
  191. // 消除模糊
  192. CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame
  193. CGFloat scaleY = height / barcodeImage.extent.size.height;
  194. CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
  195. CIContext *context = [CIContext contextWithOptions:nil];
  196. CGImageRef cgImage = [context createCGImage:transformedImage fromRect:[transformedImage extent]];
  197. UIImage* ret = [UIImage imageWithCGImage:cgImage];
  198. CGImageRelease(cgImage);
  199. return ret;
  200. }
  201. }
  202. @end