Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:steipete/PSFoundation
- Loading branch information
Showing
4 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
// | |||
// UIImage+MTTiling.h | |||
// PSFoundation | |||
// | |||
// Created by Matthias Tretter on 15.03.11. | |||
// Copyright 2011 @myell0w. All rights reserved. | |||
// | |||
// Taken from: | |||
// http://www.cimgf.com/2011/03/01/subduing-catiledlayer/ | |||
|
|||
#import <Foundation/Foundation.h> | |||
|
|||
|
|||
@interface UIImage (MTTiling) | |||
|
|||
- (void)saveTilesOfSize:(CGSize)size | |||
toDirectory:(NSString*)directoryPath | |||
usingPrefix:(NSString*)prefix; | |||
|
|||
@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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
// | |||
// UIImage+MTTiling.m | |||
// PSFoundation | |||
// | |||
// Created by Matthias Tretter on 15.03.11. | |||
// Copyright 2011 Peter Steinberger. All rights reserved. | |||
// | |||
// Taken from: | |||
// http://www.cimgf.com/2011/03/01/subduing-catiledlayer/ | |||
|
|||
#import "UIImage+MTTiling.h" | |||
|
|||
|
|||
@implementation UIImage (MTTiling) | |||
|
|||
- (void)saveTilesOfSize:(CGSize)size | |||
toDirectory:(NSString*)directoryPath | |||
usingPrefix:(NSString*)prefix | |||
{ | |||
CGFloat cols = [self size].width / size.width; | |||
CGFloat rows = [self size].height / size.height; | |||
|
|||
int fullColumns = floorf(cols); | |||
int fullRows = floorf(rows); | |||
|
|||
CGFloat remainderWidth = [self size].width - (fullColumns * size.width); | |||
CGFloat remainderHeight = [self size].height - (fullRows * size.height); | |||
|
|||
|
|||
if (cols > fullColumns) fullColumns++; | |||
if (rows > fullRows) fullRows++; | |||
|
|||
CGImageRef fullImage = [self CGImage]; | |||
|
|||
for (int y = 0; y < fullRows; ++y) { | |||
for (int x = 0; x < fullColumns; ++x) { | |||
CGSize tileSize = size; | |||
if (x + 1 == fullColumns && remainderWidth > 0) { | |||
// Last column | |||
tileSize.width = remainderWidth; | |||
} | |||
if (y + 1 == fullRows && remainderHeight > 0) { | |||
// Last row | |||
tileSize.height = remainderHeight; | |||
} | |||
|
|||
CGImageRef tileImage = CGImageCreateWithImageInRect(fullImage, | |||
(CGRect){{x*size.width, y*size.height}, | |||
tileSize}); | |||
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:tileImage]); | |||
NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png", | |||
directoryPath, prefix, x, y]; | |||
|
|||
[imageData writeToFile:path atomically:NO]; | |||
} | |||
} | |||
} | |||
|
|||
@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