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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
//  HdlCorpImageViewController.m
//  HDL_Widget_iOS
//
//  Created by HDL on 2019/10/17.
//  Copyright © 2019 JLChen. All rights reserved.
//
 
#import "HdlCorpImageViewController.h"
#import "UIImage+Crop.h"
#import "CropImageController.h"
 
#define WEAKSELF_AT __weak __typeof(&*self)weakSelf_AT = self;
 
@interface HdlCorpImageViewController ()<CropImageDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
 
@property (nonatomic, strong) NSString *mCropPicturePath;  //图片路径
@property (nonatomic, strong) NSString *mCropPictureName;  //图片命名
 
@end
 
@implementation HdlCorpImageViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _mCropPictureName = @"hdl_headPicture.Png";
    if(_openType == 101){
        [self openTypeCamera];
    }else{
        [self openTypePhotoLibrary];
    }
    
    // Do any additional setup after loading the view.
}
 
#pragma mark - 上传头像
-(void)openTypeCamera{
    WEAKSELF_AT
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = weakSelf_AT;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
 
-(void)openTypePhotoLibrary;{
    WEAKSELF_AT
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = weakSelf_AT;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}
 
#pragma mark -- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
     [picker dismissViewControllerAnimated:YES completion:nil];
//    [picker dismissViewControllerAnimated:YES completion:^{
//
//    }];
//    [picker dismissViewControllerAnimated:YES completion:^{
//        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//        CropImageController * con = [[CropImageController alloc] initWithImage:image delegate:self];
//        [self.navigationController pushViewController:con animated:YES];
//    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = image.size.height * (width/image.size.width);
    UIImage * orImage = [image resizeImageWithSize:CGSizeMake(width, height)];
    CropImageController * con = [[CropImageController alloc] initWithImage:orImage delegate:self];
    con.ovalClip = NO;
    [self.navigationController pushViewController:con animated:YES];
}
 
// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
     [self backAction:NO];
}
 
 
 
-(void)backAction:(BOOL)bGetPicture{
    if(bGetPicture){
        if(self.mSaveImageCallBack != NULL){
            [self.mSaveImageCallBack SaveImageCallBack:_mCropPicturePath];
            NSLog(@"关闭页面");
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}
 
 
#pragma mark -- CropImageDelegate
- (void)cropImageDidFinishedWithImage:(NSString *)imageName bSuccess:(BOOL)bSuccess{
 
    if(bSuccess){
        //保存到本地并返回路径
//    _mCropPicturePath
//        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//        
//        NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:
//                              [NSString stringWithFormat:@"%@", _mCropPictureName]];  // 保存文件的名称
//        
//        BOOL result =[UIImagePNGRepresentation(image)writeToFile:filePath   atomically:YES]; // 保存成功会返回YES
//        if (result == YES) {
////            _mCropPicturePath =_mCropPictureName;
//             _mCropPicturePath =filePath;
//            NSLog(@"保存成功");
//        }
   
        _mCropPicturePath = imageName;
        [self backAction:bSuccess];
    }else{
        [self backAction:bSuccess];
    }
 
}
 
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
 
@end