设置图片选择器

#import "AFHttpManager.h"
@interface EditGroupInfoViewController () 

/**
 跑团名称
 */
@property (weak, nonatomic) IBOutlet UITextView *groupNameTextView;

/**
 跑团城市
 */
@property (weak, nonatomic) IBOutlet UITextView *groupCityTextView;

/**
 跑团头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *groupImageView;
/**
 跑团宣言
 */
@property (weak, nonatomic) IBOutlet UITextView *groupManifestoTextView;

@end

@implementation EditGroupInfoViewController
{
    NSData *imageData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    DLog(@"编辑跑团界面");
//    self.title = @"编辑跑团";
    self.navigationController.title = @"编辑跑团";
    //MARK:需要修改圆形图标
    _groupImageView.layer.cornerRadius = _groupImageView.bounds.size.width/2;
    _groupImageView.layer.masksToBounds = YES;
    [self changeTextView:self.groupCityTextView];
    [self changeTextView:self.groupNameTextView];
    [self changeTextView:self.groupManifestoTextView];
    
    
}

#pragma mark - textView设置边框
- (void)changeTextView:(UITextView *)textView {
    textView.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
    textView.layer.borderWidth = 0.6f;
    textView.layer.cornerRadius = 6.0f;
}
- (IBAction)upIamgeViewAction:(id)sender {
    DLog(@"上传头像");
    
    UIActionSheet *myActionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:CustomLocalizedString(@"OpenCamera", nil),CustomLocalizedString(@"AlbumSelection",nil), nil];
    [myActionSheet showInView:self.view];
}
#pragma mark-actionSheet协议方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        NSLog(@"取消");
    }
    
    switch (buttonIndex) {
        case 0:
        {
            //打开相机
            [self takePhoto];
        }
            break;
        case 1:
        {
            //打开相册
            [self openPhoto];
        }
            break;
        default:
            break;
    }
    
}
//打开相机
-(void)takePhoto{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }else{
        NSLog(@"模拟器无法打开照相机,请在真机中使用");
    }
}
//打开相册
-(void)openPhoto{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark-图片选择协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    UIImage *image;
    if ([type isEqualToString:@"public.image"]) {
        image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        NSData *data = [NSData data];
        
        
        if (UIImagePNGRepresentation(image)==nil) {
            data = UIImageJPEGRepresentation(image, 0.1);
            imageData = data;
        }else{
            data = UIImagePNGRepresentation(image);
            imageData = data;
            
        }
        //MARK:这里要修改为上传群组的头像
//        [self requestimagedata];
        
        
        
        //将剪裁后的图片放到ImageView上
        [picker dismissViewControllerAnimated:YES completion:^{
           
            self.groupImageView.image = image;            _groupImageView.clipsToBounds = YES;
        }];
    }else{
        [picker dismissViewControllerAnimated:YES completion:^{
            NSLog(@"上传头像失败");
        }];
    }
    
}
#pragma mark - 服务器长传图片的方法,注意是群组图片
-(void)requestimagedata
{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:[ToolUtils readToken] forKey:@"token"];
    [dic setValue:@"6" forKey:@"type"];
    
    //MainUrl(@"/service/show/serviceInfoByWhere")
    [AFHttpManager postUploadWithUrl:MainUrl(@"/users/login/uploadPic") fileUrl:imageData parameter:dic success:^(id responseObject) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%@UserListbool" ,[[NSUserDefaults standardUserDefaults] objectForKey:@"identifySaves"]]];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } failure:^(NSError *error) {
        
    }];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 完成
- (IBAction)didAccomplishAction:(id)sender {
    DLog(@"点击完成按钮");
    [self.navigationController popViewControllerAnimated:YES];
}

你可能感兴趣的:(设置图片选择器)