上传图片 头像

#import "ViewController.h"

#import "AFHTTPRequestOperationManager.h"

@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate, UIPickerViewDataSource,UIPickerViewDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    _imageV.userInteractionEnabled =YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeUserIcon:)];

    [_imageVaddGestureRecognizer:tap];

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)changeUserIcon:(UITapGestureRecognizer *)sender {

    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];

    // Create the actions.

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

    }];

    //拍照

    UIAlertAction *takePictureAction = [UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

        imagePicker.delegate = self;

        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;

        imagePicker.modalTransitionStyle =UIModalTransitionStyleCoverVertical;

        imagePicker.allowsEditing = YES;

        [selfpresentViewController:imagePicker animated:YEScompletion:nil];

    }];

    

    //从手机相册选择

    UIAlertAction *pickPictureAction = [UIAlertActionactionWithTitle:@"从手机相册选择"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        //图片选择器

        UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];

        //图片来源

        picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

        picker.modalTransitionStyle =UIModalTransitionStyleCoverVertical;

        //图片是否可以编辑

        picker.allowsEditing = YES;

        picker.delegate = self;

        //切换到视图选择器

        [selfpresentViewController:picker animated:YEScompletion:nil];

    }];

    // Add the actions.

    [alertController addAction:cancelAction];

    [alertController addAction:takePictureAction];

    [alertController addAction:pickPictureAction];

    [selfpresentViewController:alertController animated:YEScompletion:nil];

}

#pragma mark - PickerDelegate图片选择

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

    UIImage *image = [infoobjectForKey:UIImagePickerControllerEditedImage];

    NSData *imageData = UIImagePNGRepresentation(image);

    [selfdismissViewControllerAnimated:YEScompletion:^{

        self.tabBarController.tabBar.hidden =NO;

        [self.imageVsetImage:image];

        NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"image.png"];

        

        

        //上传到服务器

        NSDictionary *dicP = [NSDictionarynew];

        //拼接上传所需参数,cid是从登陆成功后获取到的

    

//            dicP = @{@"cid":UserCid};

            dicP = @{@"cid":@"....."};

        

        NSString *strUrl =@"http:action";

        AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManagermanager];

        

        [manger POST:strUrl parameters:dicP constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

            [formData appendPartWithFileData:imageDataname:@"upload"fileName:filePath mimeType:@"image/png"];

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

            NSLog(@", success");

            //            [self.tableView reloadData];

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

            NSLog(@".....%@", error);

        }];

    }];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值