首页 > 代码库 > 把图片切成小片(IOS)

把图片切成小片(IOS)

把图片切成小片(IOS)

by 伍雪颖

技术分享

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

int main(int argc, const char * argv[]) {
   
@autoreleasepool{
       
if (argc < 2) {
           
return0;
        }
       
NSString*inputFile = [NSStringstringWithCString:argv[1]encoding:NSUTF8StringEncoding];
       
CGFloattitleSize = 256;
       
NSString*outputPath = [inputFile stringByDeletingPathExtension];
       
       
NSImage*image = [[NSImagealloc]initWithContentsOfFile:inputFile];
       
NSSizesize = [image size];
       
NSArray*representations = [image representations];
       
if ([representations count]) {
           
NSBitmapImageRep*representation = representations[0];
            size.
width= [representation pixelsWide];
            size.
height= [representation pixelsHigh];
        }
       
NSRectrect = NSMakeRect(0,0, size.width, size.height);
       
CGImageRefimageRef = [image CGImageForProposedRect:&rect context:NULLhints:nil];
       
       
NSIntegerrows = ceil(size.height/titleSize);
       
NSIntegercols = ceil(size.width/titleSize);
       
       
for (int y =0;y<rows;++y) {
           
for(intx=0;x<cols;++x) {
               
CGRecttitleRect = CGRectMake(x*titleSize, y*titleSize, titleSize, titleSize);
               
CGImageReftitleImage = CGImageCreateWithImageInRect(imageRef, titleRect);
               
               
NSBitmapImageRep*imageRep = [[NSBitmapImageRepalloc]initWithCGImage:titleImage];
               
NSData*data = http://www.mamicode.com/[imageRep representationUsingType:NSJPEGFileTypeproperties:nil];
               
CGImageRelease(titleImage);
               
               
NSString*path = [outputPath stringByAppendingFormat:@"_%02i_%02i.jpg",x,y];
                [data
writeToFile:path atomically:NO];
            }
        }
    }
   
return 0;
}

终端运行:
./CutImageAPP 1.jpg

把图片切成小片(IOS)