Skip to content

Instantly share code, notes, and snippets.

@mingsai
Forked from rsaunders100/UIImage+ImageSplitting.h
Created November 22, 2013 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingsai/7607702 to your computer and use it in GitHub Desktop.
Save mingsai/7607702 to your computer and use it in GitHub Desktop.
//
// UIImage+ImageSplitting.h
// Test
//
// Created by Robert Saunders on 03/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (ImageSplitting)
// Returns a NSArray of CALayers of the original image split evently into rectangles
// The possition attribute of the layers relative to the origianl image is preserved.
- (NSArray*) splitImagesIntoSubImagesWithNumberOfRows:(NSUInteger)rows numberOfColumns:(NSUInteger)clms;
@end
//
// UIImage+ImageSplitting.m
// Test
//
// Created by Robert Saunders on 03/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "UIImage+ImageSplitting.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIImage (ImageSplitting)
- (NSArray*) splitImagesIntoSubImagesWithNumberOfRows:(NSUInteger)rows numberOfColumns:(NSUInteger)clms
{
float scale = [[UIScreen mainScreen] scale];
float height = self.size.height * scale;
float width = self.size.width * scale;
NSMutableArray *subImages = [NSMutableArray array];
for(int y = 0; y < rows; y++) {
for(int x = 0; x < clms; x++) {
CGRect frame = CGRectMake((width / clms) * x,
(height / rows) * y,
(width / clms),
(height / rows));
CGImageRef subimageRef = CGImageCreateWithImageInRect(self.CGImage, frame);
UIImage* subImage = [UIImage imageWithCGImage:subimageRef scale:scale orientation:UIImageOrientationUp];
CFRelease(subimageRef);
[subImages addObject:subImage];
}
}
return subImages;
}
@end
//
// UIImage+ImageSplitting.h
// Test
//
// Created by Robert Saunders on 03/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (ImageSplitting)
// Returns a NSArray of CALayers of the original image split evently into rectangles
// The possition attribute of the layers relative to the origianl image is preserved.
- (NSArray*) splitImagesIntoSubImagesWithNumberOfRows:(NSUInteger)rows numberOfColumns:(NSUInteger)clms;
@end
//
// UIImage+ImageSplitting.m
// Test
//
// Created by Robert Saunders on 03/02/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "UIImage+ImageSplitting.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIImage (ImageSplitting)
- (NSArray*) splitImagesIntoSubImagesWithNumberOfRows:(NSUInteger)rows numberOfColumns:(NSUInteger)clms
{
float scale = [[UIScreen mainScreen] scale];
float height = self.size.height * scale;
float width = self.size.width * scale;
NSMutableArray *subImages = [NSMutableArray array];
for(int y = 0; y < rows; y++) {
for(int x = 0; x < clms; x++) {
CGRect frame = CGRectMake((width / clms) * x,
(height / rows) * y,
(width / clms),
(height / rows));
CGImageRef subimageRef = CGImageCreateWithImageInRect(self.CGImage, frame);
UIImage* subImage = [UIImage imageWithCGImage:subimageRef scale:scale orientation:UIImageOrientationUp];
CFRelease(subimageRef);
[subImages addObject:subImage];
}
}
return subImages;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment