-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+SplitImageIntoTwoParts.m
executable file
·70 lines (64 loc) · 2.35 KB
/
UIImage+SplitImageIntoTwoParts.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// UIImage+SplitImageIntoTwoParts.m
// TapRepublic
//
// Created by Weiou ZHOU on 13-5-18.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#define SAWTOOTH_COUNT 10
#define SAWTOOTH_WIDTH_FACTOR 20
#import "UIImage+SplitImageIntoTwoParts.h"
@implementation UIImage (SplitImageIntoTwoParts)
+(NSArray *)splitImageIntoTwoParts:(UIImage *)image
{
CGFloat scale = [[UIScreen mainScreen] scale];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:2];
CGFloat width,height,widthgap,heightgap;
int piceCount = SAWTOOTH_COUNT;
width = image.size.width;
height = image.size.height;
widthgap = width/SAWTOOTH_WIDTH_FACTOR;
heightgap = height/piceCount;
// CGRect rect = CGRectMake(0, 0, width, height);
CGContextRef context;
CGImageRef imageMasked;
UIImage *leftImage,*rightImage;
//part one
UIGraphicsBeginImageContext(CGSizeMake(width*scale, height*scale));
context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scale, scale);
CGContextMoveToPoint(context, 0, 0);
int a=-1;
for (int i=0; i<piceCount+1; i++) {
CGContextAddLineToPoint(context, width/2+(widthgap*a), heightgap*i);
a= a*-1;
}
CGContextAddLineToPoint(context, 0, height);
CGContextClosePath(context);
CGContextClip(context);
[image drawAtPoint:CGPointMake(0, 0)];
imageMasked = CGBitmapContextCreateImage(context);
leftImage = [UIImage imageWithCGImage:imageMasked scale:scale orientation:UIImageOrientationUp];
[array addObject:leftImage];
UIGraphicsEndImageContext();
//part two
UIGraphicsBeginImageContext(CGSizeMake(width*scale, height*scale));
context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scale, scale);
CGContextMoveToPoint(context, width, 0);
a=-1;
for (int i=0; i<piceCount+1; i++) {
CGContextAddLineToPoint(context, width/2+(widthgap*a), heightgap*i);
a= a*-1;
}
CGContextAddLineToPoint(context, width, height);
CGContextClosePath(context);
CGContextClip(context);
[image drawAtPoint:CGPointMake(0, 0)];
imageMasked = CGBitmapContextCreateImage(context);
rightImage = [UIImage imageWithCGImage:imageMasked scale:scale orientation:UIImageOrientationUp];
[array addObject:rightImage];
UIGraphicsEndImageContext();
return array;
}
@end