| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // ImageUtils.m
- // AntsContract
- //
- // Created by Ray on 12/20/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "ImageUtils.h"
- #import "AppDelegate.h"
- @implementation ImageUtils
- +(UIImage*)img_compress:(UIImage*)image kbsize:(float) size
- {
-
-
-
- //UIImage *image=[UIImage imageNamed:@"xxoo.jpeg"];
- NSData *imageData=UIImageJPEGRepresentation(image, 1.f);
-
- if(size>imageData.length/1024)
- return image;
-
- // CGFloat size=40.f;// kb
- CGFloat scale=size/(imageData.length/1024);
-
- scale = sqrt (scale);
-
- CGSize newsize=image.size;
- newsize.height = newsize.height*scale;
- newsize.width = newsize.width*scale;
-
- return [self scaleImageToSize:image size:newsize];
- // NSData *newData=UIImageJPEGRepresentation(image, scale);
-
- // UIImage* ret= [[UIImage alloc] initWithData:newData];
- //
- // return ret;
- }
- + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path size:(CGSize)size allow_enlarge:(bool)allow_enlarge
- {
-
- NSData* data=[self load_cached_img:filename loadFrom:path];
-
- return data;
- // UIImage* img = [UIImage imageWithData:data];
- // if(!allow_enlarge&&(size.width>img.size.width|| size.height>img.size.height))
- // return data;
- //
- //
- //// CGRect rect = [self scaleToSize:CGRectMake(0, 0, img.size.width, img.size.height) to:size];
- // UIImage* scaledimg=[self scaleImageToSize:img size:size];
- //
- // NSData *scaledData = UIImagePNGRepresentation(scaledimg);
- // return scaledData;
- }
- + (NSData*) load_cached_img:(NSString*) filename loadFrom:(NSString*) path
- {
- if(path.length==0)
- return nil;
-
- path=[path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
- path=[path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
- // path=[path stringByReplacingOccurrencesOfString:filename withString:@""];
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- if(appDelegate.bEnable_Cache==false)
- return nil;
-
-
- NSData* data = nil;
-
-
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachefolder = [paths objectAtIndex:0];
- NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
- NSString *filePath = [img_cache stringByAppendingPathComponent:path];
-
- NSFileManager* fileManager = [NSFileManager defaultManager];
- if( [fileManager fileExistsAtPath:filePath ])
- {
- data = [NSData dataWithContentsOfFile: filePath];
- }
- return data;
-
- }
- + (CGRect)scaleToSize:(CGRect )from to:(CGSize)to
- {
- if(from.size.width/from.size.height>to.width/to.height)
- {
- return CGRectMake(from.origin.x, from.origin.y, to.width, to.width*from.size.height/from.size.width);
- }
- else
- {
- return CGRectMake(from.origin.x, from.origin.y, to.height*from.size.width/from.size.height, to.height);
- }
- // // 创建一个bitmap的context
- // // 并把它设置成为当前正在使用的context
- // UIGraphicsBeginImageContext(size);
- // // 绘制改变大小的图片
- // [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
- // // 从当前context中创建一个改变大小后的图片
- // UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- // // 使当前的context出堆栈
- // UIGraphicsEndImageContext();
- // // 返回新的改变大小后的图片
- //
- // // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
- // return scaledImage;
- }
- + (UIImage *)scaleImageToSize:(UIImage *)img size:(CGSize)size{
- // 创建一个bitmap的context
- // 并把它设置成为当前正在使用的context
- UIGraphicsBeginImageContext(size);
- // 绘制改变大小的图片
- [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
- // 从当前context中创建一个改变大小后的图片
- UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
- // 使当前的context出堆栈
- UIGraphicsEndImageContext();
- // 返回新的改变大小后的图片
-
- // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
- return scaledImage;
- }
- + (CGRect)rectAlign:(CGRect )parent rect:(CGRect)rect hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
- {
- // double cx=parent.origin.x+parent.size.width/2;
- // double cy=parent.origin.y+parent.size.height/2;
- CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
- if([hAlign.lowercaseString isEqualToString:@"center"])
- {
- rect=CGRectMake(centerpoint.x-rect.size.width/2, rect.origin.y, rect.size.width, rect.size.height);
- }
- else
- if([hAlign.lowercaseString isEqualToString:@"left"])
- {
- rect=CGRectMake(parent.origin.x, rect.origin.y, rect.size.width, rect.size.height);
- }
- else
- if([hAlign.lowercaseString isEqualToString:@"right"])
- {
- rect=CGRectMake(parent.origin.x+parent.size.width-rect.size.width, rect.origin.y, rect.size.width, rect.size.height);
- }
- if([vAlign.lowercaseString isEqualToString:@"middle"])
- {
- rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
- }
-
- return rect;
-
- }
- + (NSData*) load_img:(NSString*) filename
- {
- if(filename.length==0)
- return nil;
-
- NSData* data = nil;
- //
- //
- // NSString* tempDir = NSTemporaryDirectory();
- // NSString *filePath = [tempDir stringByAppendingPathComponent:filename];
- //
- NSFileManager* fileManager = [NSFileManager defaultManager];
- if( [fileManager fileExistsAtPath:filename ])
- {
- data = [NSData dataWithContentsOfFile: filename];
- }
-
-
- return data;
-
- }
- +(UIImage*)generateBarCode:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
- {
- //return nil;
- // 生成二维码图片
- CIImage *barcodeImage;
- NSData *data = [barCodeStr dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
- CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
-
- [filter setValue:data forKey:@"inputMessage"];
- barcodeImage = [filter outputImage];
-
- // 消除模糊
- CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame
- CGFloat scaleY = height / barcodeImage.extent.size.height;
- CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
- CIContext *context = [CIContext contextWithOptions:nil];
- CGImageRef cgImage = [context createCGImage:transformedImage fromRect:[transformedImage extent]];
-
- UIImage* ret = [UIImage imageWithCGImage:cgImage];
-
-
- CGImageRelease(cgImage);
-
- return ret;
-
- }
- +(NSString*)generateBarCodeFile:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
- {
- UIImage* img = [self generateBarCode:barCodeStr width:width height:height];
- NSString* qrpath=nil;
- NSString* temp = NSTemporaryDirectory();
- NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
- qrpath=[temp stringByAppendingPathComponent:filename];
-
- [UIImagePNGRepresentation(img)writeToFile: qrpath atomically:YES];
- return qrpath;
- }
- +(NSString*)generateQRCodeFile:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
- {
-
- UIImage* img = [self generateQRCode:barCodeStr width:width height:height];
- NSString* qrpath=nil;
- NSString* temp = NSTemporaryDirectory();
- NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
- qrpath=[temp stringByAppendingPathComponent:filename];
-
- [UIImagePNGRepresentation(img)writeToFile: qrpath atomically:YES];
- return qrpath;
- }
- +(UIImage*)generateQRCode:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height
- {
- {
- // 生成二维码图片
- CIImage *barcodeImage;
- NSData *data = [barCodeStr dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
- CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
-
- [filter setValue:data forKey:@"inputMessage"];
- barcodeImage = [filter outputImage];
-
- // 消除模糊
- CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame
- CGFloat scaleY = height / barcodeImage.extent.size.height;
- CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
-
- CIContext *context = [CIContext contextWithOptions:nil];
- CGImageRef cgImage = [context createCGImage:transformedImage fromRect:[transformedImage extent]];
-
- UIImage* ret = [UIImage imageWithCGImage:cgImage];
-
-
- CGImageRelease(cgImage);
-
- return ret;
-
- }
- }
- @end
|