Created
February 3, 2012 15:02
-
-
Save rsaunders100/1730582 to your computer and use it in GitHub Desktop.
(iOS) Split an image into an array of evenly sized CALayers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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