我们公司有大量的图片需要处理,简单的说,是将不规则的图片处理成,符合我们要求的图片,当然:我们有需求,需求如下(很明显这个需求是一个妹子写的)
我尝试过使用Java 语言,但是我最后还是使用OC,先来看下效果
第一步:选择文件夹
-(void)action1:(id)sender{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setMessage:@"请选择目录"];
[panel setCanChooseDirectories:YES];
[panel setCanCreateDirectories:YES];
[panel setCanChooseFiles:NO];
[panel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton)
{
NSURL* url = [panel URL];
NSString *str = [url absoluteString];
[shuruString setString:[[self decodeString:str] substringFromIndex:7]];
}
}];
}
-(NSString *)decodeString:(NSString*)encodedString
{
NSString *decodedString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(__bridge CFStringRef)encodedString,
CFSTR(""),
CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
-(void)listFileInPath:(NSString *)path{
NSFileManager *file = [NSFileManager defaultManager];
NSArray *arr1 = [file contentsOfDirectoryAtPath:path error:NULL];
for (NSString *fName in arr1) {
BOOL isDir = NO;
NSString *luString = [path stringByAppendingPathComponent:fName];
[file fileExistsAtPath:luString isDirectory:&isDir];
NSString *newPath = [shuchuString stringByAppendingPathComponent:[luString stringByReplacingOccurrencesOfString:shuruString withString:@""]];
if (isDir) {
if (![file fileExistsAtPath:newPath]) {
[file createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
}
}
if (isDir) {
[self listFileInPath:luString];
}else{
[self chuliWenJian:luString shuruWenjian:fName shuChuLujing:newPath];
}
}
}
-(void)chuliWenJian:(NSString *)shurujueduiLujing shuruWenjian:(NSString *)wenjianMing shuChuLujing:(NSString *)shuChuLuJing{
NSArray *fileNames = @[@".jpg",@".png",@".jpeg",@".JGP",@".PNG",@".JPEG"];
BOOL isImage = NO;
for (NSString *s in fileNames) {
if ([wenjianMing hasSuffix:s]) {
isImage = YES;
break;
}
}
if (!isImage) {
return;
}
NSData *d = [NSData dataWithContentsOfFile:shurujueduiLujing];
NSImage *img = [[NSImage alloc] initWithData:d];
CGFloat length1000 = 1000.0,lenth750=750.0,leng562=562.0;
CGFloat imgw = img.size.width;
CGFloat imgh = img.size.height;
CGFloat f = imgw*1.0/imgh;
CGRect rect = CGRectZero;
CGSize size = CGSizeZero;
if (f<=0.75) {
/// 长图 干掉上下
/// 裁剪后 宽 imgw 高 imgw * length1000/lenth750
rect = CGRectMake(0, (imgh-imgw * length1000/lenth750)/2, imgw, imgw * length1000/lenth750);
size = CGSizeMake(lenth750, length1000);
}else if (f>0.75&&f<=0.875){
/// 长图
rect = CGRectMake((imgw - imgh*lenth750/length1000)/2, 0, imgh*lenth750/length1000, imgh);
size = CGSizeMake(lenth750, length1000);
}else if (f>0.875&&f<=1.15){
if (f>=1) {
rect = CGRectMake((imgw - imgh)/2, 0, imgh, imgh);
}else{
rect = CGRectMake(0, (imgh-imgw)/2, imgw, imgw );
}
size = CGSizeMake(lenth750, lenth750);
}else if (f>1.15&&f<=1.33452){
/// 宽图
rect = CGRectMake(0, (imgh-imgw * leng562/lenth750)/2, imgw, imgw * leng562/lenth750);
size = CGSizeMake(lenth750, leng562);
}else if (f>1.33452){
/// 宽图
/// 长图
rect = CGRectMake((imgw - imgh*lenth750/leng562)/2, 0, imgh*lenth750/leng562, imgh);
size = CGSizeMake(lenth750, leng562);
}
[self tupianChuliZhuYao:size rect2:rect immmm:img xinLujing:shuChuLuJing];
}
-(void)tupianChuliZhuYao:(NSSize)rect1Mubiao rect2:(NSRect )rect2Caijian immmm:( NSImage *)img xinLujing:(NSString *)xingLujing{
if (rect1Mubiao.height == 0) {
return;
}
if (rect2Caijian.size.width == 0) {
return;
}
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:rect1Mubiao.width
pixelsHigh:rect1Mubiao.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
rep.size = rect1Mubiao;
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:rep]];
[img drawInRect:NSMakeRect(0, 0, rect1Mubiao.width, rect1Mubiao.height) fromRect:rect2Caijian operation:NSCompositingOperationCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
NSImage *newImage = [[NSImage alloc] initWithSize:rect1Mubiao];
[newImage addRepresentation:rep];
NSImage *immmm = newImage;
NSData *imageData = [immmm TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = nil;
NSNumber *quality = [NSNumber numberWithFloat:.85];
imageProps = [NSDictionary dictionaryWithObject:quality forKey:NSImageCompressionFactor];
NSData *ddddd = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[ddddd writeToFile:xingLujing atomically:YES];
}
github更新中:https://github.com/hebiao6446/PictureProcessing