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