-
Notifications
You must be signed in to change notification settings - Fork 3
/
UIImage+PECrop.m
49 lines (36 loc) · 1.59 KB
/
UIImage+PECrop.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
//
// UIImage+PECrop.m
// PhotoCropEditor
//
// Created by Ernesto Rivera on 2013/07/29.
// Copyright (c) 2013 kishikawa katsumi. All rights reserved.
//
#import "UIImage+PECrop.h"
@implementation UIImage (PECrop)
- (UIImage *)rotatedImageWithtransform:(CGAffineTransform)rotation
croppedToRect:(CGRect)rect
{
UIImage *rotatedImage = [self pe_rotatedImageWithtransform:rotation];
CGFloat scale = rotatedImage.scale;
CGRect cropRect = CGRectApplyAffineTransform(rect, CGAffineTransformMakeScale(scale, scale));
CGImageRef croppedImage = CGImageCreateWithImageInRect(rotatedImage.CGImage, cropRect);
UIImage *image = [UIImage imageWithCGImage:croppedImage scale:self.scale orientation:rotatedImage.imageOrientation];
CGImageRelease(croppedImage);
return image;
}
- (UIImage *)pe_rotatedImageWithtransform:(CGAffineTransform)transform
{
CGSize size = self.size;
UIGraphicsBeginImageContextWithOptions(size,
YES, // Opaque
self.scale); // Use image scale
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, size.width / 2, size.height / 2);
CGContextConcatCTM(context, transform);
CGContextTranslateCTM(context, size.width / -2, size.height / -2);
[self drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rotatedImage;
}
@end