关闭

上传图片 头像

标签: iOS上传图片
155人阅读 评论(0) 收藏 举报
分类:

#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);

        }];

    }];

}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27511次
    • 积分:680
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:21篇
    • 译文:0篇
    • 评论:11条
    最新评论