Mac图片处理工具

我们公司有大量的图片需要处理,简单的说,是将不规则的图片处理成,符合我们要求的图片,当然:我们有需求,需求如下(很明显这个需求是一个妹子写的)



我尝试过使用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]];
            
            
        }
    }];
    
}

注意,这里有个URL编解码的过程,因为有可能会出现汉字目录,

-(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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值