//
//  UIViewController+buttonUtill.m
//  Open
//
//  Created by 看看智能 on 2018/1/10.
//  Copyright © 2018年 黄江涛 . All rights reserved.
//

#import "UIViewController+buttonUtill.h"
static const void *exterHSx_1 = &exterHSx_1;
static const void *exterHSx_2 = &exterHSx_2;

static const void *exterHSx_A = &exterHSx_A;
static const void *exterHSx_B = &exterHSx_B;
static const void *exterHSx_C = &exterHSx_C;

@implementation UIViewController (buttonUtill)


-(void)setButtonS:(void (^)(CLMarkButton *))buttonS
{
      objc_setAssociatedObject(self, exterHSx_1, buttonS, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(void (^)(CLMarkButton *))buttonS
{
        return objc_getAssociatedObject(self,exterHSx_1 );
    
}

//A
-(void)setButtonA:(void (^)(CLMarkButton *))buttonA
{
    objc_setAssociatedObject(self, exterHSx_A, buttonA, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(void (^)(CLMarkButton *))buttonA
{
    return objc_getAssociatedObject(self,exterHSx_A);
}

//B
-(void)setButtonB:(void (^)(CLMarkButton *))buttonB
{
    objc_setAssociatedObject(self, exterHSx_B, buttonB, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(void (^)(CLMarkButton *))buttonB
{
    return objc_getAssociatedObject(self,exterHSx_B);
    
}


//X
-(void)setButtonX:(void (^)(CLMarkButton *))buttonX
{
    objc_setAssociatedObject(self, exterHSx_C, buttonX, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(void (^)(CLMarkButton *))buttonX
{
    return objc_getAssociatedObject(self,exterHSx_C);
    
}








-(void)setNextB:(void (^)(BOOL))nextB
{
    objc_setAssociatedObject(self, exterHSx_2, nextB, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
}
-(void (^)(BOOL))nextB
{
    return objc_getAssociatedObject(self,exterHSx_2);
    
}






-(void)textFieldAddTwoBlackPlaceinTextFiled:(UITextField *)textField
{
    UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(textField.frame.origin.x,textField.frame.origin.y,10.0, textField.frame.size.height)];
    textField.leftView = blankView;
    textField.leftViewMode =UITextFieldViewModeAlways;
}
-(void)buttonUse:(NSArray*)arr andID:(NSInteger)ID
{
  
    
    
    
    
    
    
    for (int i=0; i<arr.count; ++i)
    {
        CLMarkButton *button = arr[i];
        button.groupId = ID;
        button.type = CLMarkTypeSingleRadio;
        
       
        button.tag = i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}


- (void)buttonClick:(CLMarkButton *)sender
{
    [sender buttonDidClick];
    
    if (sender.groupId ==13007 || sender.groupId ==10007  || sender.groupId ==13007 || sender.groupId ==11209 ) {
         self.buttonS(sender);
    }else if (sender.groupId == 14007|| sender.groupId == 8478){
           self.buttonA(sender);
        
    }else if (sender.groupId == 545848 || sender.groupId == 149346357){
         self.buttonB(sender);
        
    }else if (sender.groupId == 235452 ){
        self.buttonX(sender);
    }

   
    
}




-(void)jiantingALL
{
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressChoose:) name:@"KankanNextButtom" object:nil];
    
}


#pragma mark - textfieldDelegate

-(void)addressChoose:(NSNotification*)not{
    
    NSLog(@"sdsad%@",not.object);
    
    NSString * ingsf =(NSString *)not.object;
        BOOL YesNext;
        if ([ingsf isEqualToString:@"next"]) {
            YesNext =YES;
            self.nextB(YesNext);
            
        }else{
            YesNext =NO;
             self.nextB(YesNext);
            
        }   
}



-(NSString *)changImageBase64:(UIImage *)image
{
    CGSize imagesize = image.size;
    NSLog(@"%lf",image.size.height);
    NSLog(@"%lf",image.size.width);
    imagesize.height =50;
    imagesize.width =50;
    //对图片大小进行压缩--
    
    //    UIImage *image1=   [SecondSocialViewController imageCompressFitSizeScale:image targetSize:CGSizeMake(image.size.width/4, image.size.height/4)];
    
    UIImage *image1=    [self imageCompressForSize:image targetSize:CGSizeMake(483, 644)];
    //照片是对的完全没有问题,如果想加快速度必须让戴海峰配合的去弄,否则毫无办法,因为把他放在网上转base64是可以转出来的,如果想提速只要调用上面的这个方法即可.否则毫无办法,上面两个方法都是可以的
    //这里的0.3决定了照片认证的精确值
    image1 =   [SecondSocialViewController image:image1 rotation:UIImageOrientationLeft];
    
    NSData *str2=UIImageJPEGRepresentation(image1, 0.4);
    
    //    //test------------------------------
    //    image1 =nil;
    //    imag1 = [UIImage imageWithData:str2];

    //test----------------------------------------------------------------------------------------------------------------------------------
    image1 = nil;
    image1 = [UIImage imageWithData:str2];
    //这里面我放了测试代码
    
//    NSString *targetPath_3333=[self.documentsPath_3 stringByAppendingPathComponent:@"12yue13ri采集照片_2fgdfgdfg.jpg"];
//    [UIImagePNGRepresentation(image1) writeToFile:targetPath_3333 atomically:YES];
    //test----------------------------------------------------------------------------------------------------------------------------------
    
    //    NSData *str2 =UIImagePNGRepresentation(image1);
    NSString *str3=[EnercryTool encryptBase64:str2];
    NSLog(@"%ld",(unsigned long)[str3 length]);
    return str3;
}



-(UIImage *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
    
    
    UIImage *newImage = nil;
    
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = size.width;
    
    CGFloat targetHeight = size.height;
    
    CGFloat scaleFactor = 0.0;
    
    CGFloat scaledWidth = targetWidth;
    
    CGFloat scaledHeight = targetHeight;
    
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
    
    if(CGSizeEqualToSize(imageSize, size) == NO){
        
        
        CGFloat widthFactor = targetWidth / width;
        
        CGFloat heightFactor = targetHeight / height;
        
        
        if(widthFactor > heightFactor){
            
            scaleFactor = widthFactor;
            
            
        }
        
        else{
            
            
            scaleFactor = heightFactor;
            
        }
        
        scaledWidth = width * scaleFactor;
        
        scaledHeight = height * scaleFactor;
        
        
        if(widthFactor > heightFactor){
            
            
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            
        }else if(widthFactor < heightFactor){
            
            
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            
        }
        
    }
    
    
    UIGraphicsBeginImageContext(size);
    //UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:size];
    
    CGRect thumbnailRect = CGRectZero;
    
    thumbnailRect.origin = thumbnailPoint;
    
    thumbnailRect.size.width = scaledWidth;
    
    thumbnailRect.size.height = scaledHeight;
    
    
    [sourceImage drawInRect:thumbnailRect];
    
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        
        NSLog(@"scale image fail");
        
    }
    
    
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation

{
    
    long double rotate = 0.0;
    
    CGRect rect;
    
    float translateX = 0;
    
    float translateY = 0;
    
    float scaleX = 1.0;
    
    float scaleY = 1.0;
    
    
    
    switch (orientation) {
            
        case UIImageOrientationLeft:
            
            rotate = M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = 0;
            
            translateY = -rect.size.width;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationRight:
            
            rotate = 3 * M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = -rect.size.height;
            
            translateY = 0;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationDown:
            
            rotate = M_PI;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = -rect.size.width;
            
            translateY = -rect.size.height;
            
            break;
            
        default:
            
            rotate = 0.0;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = 0;
            
            translateY = 0;
            
            break;
            
    }
    
    
    
    //UIGraphicsBeginImageContext(rect.size);
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:rect.size];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //做CTM变换
    
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextRotateCTM(context, rotate);
    
    CGContextTranslateCTM(context, translateX, translateY);
    
    
    
    CGContextScaleCTM(context, scaleX, scaleY);
    
    //绘制图片
    
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    
    
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    return newPic;
    
}




@end
