-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+ImagePickerCrop.m
47 lines (36 loc) · 1.79 KB
/
UIImage+ImagePickerCrop.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
//
// UIImage+ImagePickerCrop.m
// ImagePickerCropDemo
//
// Created by Frank Schmitt on 4/1/14.
// Copyright (c) 2014 Frank Schmitt. See LICENSE.
//
#import "UIImage+ImagePickerCrop.h"
@implementation UIImage (ImagePickerCrop)
+ (UIImage *)croppedImageWithImagePickerInfo:(NSDictionary *)info {
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect cropRect = [[info objectForKey:UIImagePickerControllerCropRect] CGRectValue];
CGAffineTransform rotateTransform = CGAffineTransformIdentity;
switch (originalImage.imageOrientation) {
case UIImageOrientationDown:
rotateTransform = CGAffineTransformRotate(rotateTransform, M_PI);
rotateTransform = CGAffineTransformTranslate(rotateTransform, -originalImage.size.width, -originalImage.size.height);
break;
case UIImageOrientationLeft:
rotateTransform = CGAffineTransformRotate(rotateTransform, M_PI_2);
rotateTransform = CGAffineTransformTranslate(rotateTransform, 0.0, -originalImage.size.height);
break;
case UIImageOrientationRight:
rotateTransform = CGAffineTransformRotate(rotateTransform, -M_PI_2);
rotateTransform = CGAffineTransformTranslate(rotateTransform, -originalImage.size.width, 0.0);
break;
default:
break;
}
CGRect rotatedCropRect = CGRectApplyAffineTransform(cropRect, rotateTransform);
CGImageRef croppedImage = CGImageCreateWithImageInRect([originalImage CGImage], rotatedCropRect);
UIImage *result = [UIImage imageWithCGImage:croppedImage scale:[UIScreen mainScreen].scale orientation:originalImage.imageOrientation];
CGImageRelease(croppedImage);
return result;
}
@end