ios 开发选取头像,图片库,相机,裁取图片

需要遵守的代理协议:UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate

@property (nonatomic, strong) UIActionSheet *avatarActionSheet;

第一步:点击头像cell需要做的事情

[self.avatarActionSheet showInView:self.view];

 

第二步:

#pragma mark - 点击头像

- (UIActionSheet *)avatarActionSheet

{

    if (!_avatarActionSheet) {

        _avatarActionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"选择你的头像", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"取消", @"") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"相机", @""), NSLocalizedString(@"在图片库选择", @""), nil];

    }

    return _avatarActionSheet;

}

 

#pragma mark - 弹框代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    GYLog(@"%d" , buttonIndex);

    

    if (buttonIndex == 2) return;// 取消

    [self showImagePickerViewController:buttonIndex];

}

 

- (void)showImagePickerViewController:(NSInteger)pickerUserAvatarType

{

    UIImagePickerControllerSourceType sourceType;

    NSString *description = nil;

    switch (pickerUserAvatarType) {

        case 0:

            sourceType = UIImagePickerControllerSourceTypeCamera;

            description = NSLocalizedString(@"该设备不支持相机", @"");

            break;

        case 1:

            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            description = NSLocalizedString(@"该设备不支持图片库", @"");

            break;

    }

    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {

        [self initalizerAlertViewWithTitle:NSLocalizedString(@"警告", @"") description:description];

        return;

    }

    

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;

    imagePickerController.allowsEditing = YES;

    imagePickerController.sourceType = sourceType;

    imagePickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;

    [self presentViewController:imagePickerController animated:YES completion:NULL];

}

 

- (void)initalizerAlertViewWithTitle:(NSString *)title description:(NSString *)description

{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:description delegate:nil cancelButtonTitle:nil otherButtonTitles: NSLocalizedString(@"确定", @""), nil];

    [alertView show];

}

 

- (void)showAvatarImage:(UITapGestureRecognizer *)tapGesture

{

    GYLog(@"----------------------");

}

 

#pragma mark - 图片选择控制器的代理

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    UIImage *userAvatar = [info valueForKey:UIImagePickerControllerEditedImage];

    [self.userIconView setImage:userAvatar];

    [picker dismissViewControllerAnimated:YES completion:NULL];

   

    // 1.创建请求管理对象

    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    

    // 2.封装请求参数

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"verify"] = [GYUserDefaults objectForKey:@"verify"];

    params[@"avatar"] = userAvatar;

    

    // 3.发送请求

    NSString *str = [NSString stringWithFormat:@"http://%@/?action=personal&option=update_avatar",GYAPP_URL];

    [mgr POST:str parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { // 在发送请求之前调用这个block

 

            NSData *data = UIImageJPEGRepresentation(userAvatar, 0.000001);

            [formData appendPartWithFileData:data name:@"pic" fileName:@"" mimeType:@"image/jpeg"];

    

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        

        [MBProgressHUD showSuccess:@"发送成功"];

#warning 是否需要进行数据存储

        [GYUserDefaults setObject:responseObject[@"avatar"] forKey:@"avatar"];

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        

        [MBProgressHUD showError:@"发送失败"];

        

    }];

 

}

 

 

ios 开发选取头像,图片库,相机,裁取图片,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。