IVTMyFeedBackViewController.m 10 KB
//
//  IVTMyFeedBackViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMyFeedBackViewController.h"

@interface IVTMyFeedBackViewController ()<UITextViewDelegate,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UITextView *feedBackTextView;
    UILabel *hintLabel;
    
    UIButton *addPhotoButton;
    
    UITextField *contactLabel;
    
    NSData *formImageData;
}
@end

@implementation IVTMyFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavigationBar];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, kScreenWidth, 183)];
    headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    
    feedBackTextView = [[UITextView alloc] initWithFrame:CGRectMake(25, 10, kScreenWidth-25*2, 90)];
    feedBackTextView.backgroundColor = [UIColor whiteColor];
    feedBackTextView.font = [UIFont YXFontOfSize:14];
    feedBackTextView.textColor = [UIColor YXColorWithHexCode:@"#8C8C8C"];
    feedBackTextView.delegate = self;
    feedBackTextView.autocorrectionType = UITextAutocorrectionTypeNo;//不联想输入
    feedBackTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;//不大小写
    [headView addSubview:feedBackTextView];
    
    hintLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 0, feedBackTextView.frame.size.width-3, 50)];
    hintLabel.backgroundColor = [UIColor clearColor];
    hintLabel.font = [UIFont YXFontOfSize:14];
    hintLabel.textColor = [UIColor YXColorWithHexCode:@"#8C8C8C"];
    hintLabel.text = @"请描述您遇到的问题的功能名称,以及问题现象,同时可以上传手机截图(必填)";
    hintLabel.numberOfLines = 2;
    [feedBackTextView addSubview:hintLabel];
    
    addPhotoButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    addPhotoButton.frame = CGRectMake(28, 110, 56, 56);
    [addPhotoButton setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
    [addPhotoButton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:addPhotoButton];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, kScreenWidth, 46)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *link = [[UILabel alloc]initWithFrame:CGRectMake(28, 13, 57, 20)];
    link.text = @"联系方式";
    link.textColor = [UIColor IVTMainTextColor];
    link.font = [UIFont YXFontOfSize:14];
    [footView addSubview:link];
    
    contactLabel = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, kScreenWidth-120, 46)];
    contactLabel.font = [UIFont YXFontOfSize:14];
    contactLabel.backgroundColor = [UIColor whiteColor];
    contactLabel.textColor = [UIColor IVTMainTextColor];
    contactLabel.placeholder =@"邮箱/QQ/手机号码(必填)";
    //修改textField提示文字颜色
    [contactLabel setValue:[UIColor IVTSeparatorColor] forKeyPath:@"_placeholderLabel.textColor"];
    contactLabel.delegate = self;
    [footView addSubview:contactLabel];
    
    
    UIButton *feed = [UIButton buttonWithType:UIButtonTypeCustom];
    feed.frame = CGRectMake(15, 300, kScreenWidth - 15*2, 35);
    [feed setBackgroundImage:[UIImage imageNamed:@"提交反馈按钮"] forState:UIControlStateNormal];
    [feed setTitle:@"提交反馈" forState:UIControlStateNormal];
    [feed setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    feed.titleLabel.font  = [UIFont YXFontOfSize:14];
    [feed addTarget:self action:@selector(feedMessage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:feed];

    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
}

- (void)setNavigationBar{
    self.navigationItem.title = @"意见反馈";
}
#pragma mark - textView代理
- (void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length == 0){
        hintLabel.hidden = NO;
    }else{
        hintLabel.hidden = YES;
    }
}

- (void)photoClick{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] && ![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"没找到摄像头" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
            }];
        }
        else{
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *imagePicker = [self imageWithImage:info[@"UIImagePickerControllerEditedImage"] scaledToSize:CGSizeMake(400, 400)];
    formImageData = UIImageJPEGRepresentation(imagePicker, 0.4);
    UIImage *image = [UIImage imageWithData:formImageData];
    [addPhotoButton setImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)targetSize{
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (!CGSizeEqualToSize(imageSize, targetSize))
    {
        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(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)feedMessage:(UIButton *)btn{
    if (feedBackTextView.text.length > 0) {
        if (contactLabel.text.length > 0) {
            [HUDUtil showLoading];
            btn.enabled = NO;
            [[IVTNetworkAPIClient sharedClient] feedback:[feedBackTextView.text base64EncodedFromString] formData:formImageData success:^(NSDictionary *successData) {
                [HUDUtil showSuccess:@"反馈成功"];

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } error:^(NSDictionary *successData) {
                NSString *msg = successData[@"description"];
                [HUDUtil showError:msg];
                btn.enabled = YES;
            } failure:^(NSError *failureError) {
                [HUDUtil showError:@"网络异常"];
                btn.enabled = YES;
            }];
        }
        else{
            [HUDUtil showError:@"请输入联系方式"];
        }
    }
    else{
        [HUDUtil showError:@"反馈不能为空"];
    }
}

-(void)viewTapped:(UITapGestureRecognizer *)tap{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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