wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  HQEditImageCaptureView.m
//  CivilAviation
//
//  Created by iOS on 2019/4/1.
//  Copyright © 2019 iOS. All rights reserved.
//
 
#import "HQEditImageCaptureView.h"
 
@implementation HQEditImageCaptureView
 
- (instancetype)init {
    if (self = [super init]) {
        self.layer.masksToBounds = NO;
    }
    return self;
}
 
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view) {
        return view;
    } else {
        return self.captureView;
    }
}
 
- (UIImage *)captureImage {
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width,self.frame.size.height), NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return viewImage;
}
 
- (UIImage *)captureOriginalImage {
    UIScrollView *scrollView = (UIScrollView *)self.captureView;
    UIImage *orignaImage = self.imageView.image;
    
    CGFloat width = (self.captureView.frame.size.width/self.imageView.frame.size.width)*orignaImage.size.width;
    CGFloat height = (scrollView.frame.size.height/scrollView.frame.size.width)*width;
    CGSize captureSize = CGSizeMake(width, height);
    
    CGFloat x = (scrollView.contentOffset.x/self.imageView.frame.size.width)*orignaImage.size.width;
    CGFloat y = (scrollView.contentOffset.y/self.imageView.frame.size.height)*orignaImage.size.height;
    CGPoint captureOffset = CGPointMake(x, y);
    
    // 长宽微调
    if (x + width >= self.imageView.frame.size.width) {
        x = self.imageView.frame.size.width - width;
    }
    if (y + height >= self.imageView.frame.size.height) {
        y = self.imageView.frame.size.height - height;
    }
    
    CGRect captureRect;
    if (self.rotateTimes % 2 == 0) {
        captureRect = CGRectMake(captureOffset.x, captureOffset.y, captureSize.width, captureSize.height);
    } else {
        captureRect = CGRectMake(captureOffset.x, captureOffset.y, captureSize.height, captureSize.width);
    }
    
    CGImageRef temp = CGImageCreateWithImageInRect(orignaImage.CGImage, captureRect);
    UIImage *result = [UIImage imageWithCGImage:temp];
    CGImageRelease(temp);
    
    result = [self rotateImage:result times:self.rotateTimes];
    
    return result;
}
 
- (UIImage *)rotateImage:(UIImage *)image times:(NSInteger)rotateTimes {
    if (rotateTimes % 4 == 0) {
        return image;
    } else if (rotateTimes % 4 == 1) {
        return [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationLeft];
    } else if (rotateTimes % 4 == 2) {
        return [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationDown];
    } else {
        return [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight];
    }
}
 
@end