Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created February 3, 2012 15:02
Show Gist options
  • Save rsaunders100/1730582 to your computer and use it in GitHub Desktop.
Save rsaunders100/1730582 to your computer and use it in GitHub Desktop.
(iOS) Split an image into an array of evenly sized CALayers
//
// 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