Cut the image into small pieces (IOS)

Source: Internet
Author: User

Cut the image into small pieces (IOS)
Cut the image into small pieces (IOS)

# Import # Import

Int main (int argc, const char * argv []) {
@ Autoreleasepool {
If (argc <2 ){
Return 0;
}
NSString * inputFile = [NSString stringWithCString: argv [1] encoding: NSUTF8StringEncoding];
CGFloat titleSize = 256;
NSString * outputPath = [inputFile stringByDeletingPathExtension];

NSImage * image = [[NSImage alloc] initWithContentsOfFile: inputFile];
NSSize size = [image size];
NSArray * representations = [image representations];
If ([representations count]) {
NSBitmapImageRep * representation = representations [0];
Size. width = [representation pixelsWide];
Size. height = [representation pixelsHigh];
}
NSRect rect = NSMakeRect (0, 0, size. width, size. height );
CGImageRef imageRef = [image CGImageForProposedRect: & rect context: NULL hints: nil];

NSInteger rows = ceil (size. height/titleSize );
NSInteger cols = ceil (size. width/titleSize );

For (int y = 0; y For (int x = 0; x CGRect titleRect = CGRectMake (x * titleSize, y * titleSize, titleSize, titleSize );
CGImageRef titleImage = CGImageCreateWithImageInRect (imageRef, titleRect );

NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc] initWithCGImage: titleImage];
NSData * data = [imageRep representationUsingType: ns1_filetype properties: nil];
CGImageRelease (titleImage );

NSString * path = [outputPath stringByAppendingFormat: @ _ % 02i_%02i.jpg, x, y];
[Data writeToFile: path atomically: NO];
}
}
}
Return 0 ;}
Terminal run:./CutImageAPP 1.jpg

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.