首页 > 代码库 > 把图片切成小片(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;
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。