ios 单张图片上传(从相册或者相机)

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong)UIButton * imageButton;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"图片上传";
    // 创建视图
    [self createViewAbout];
}

#pragma mark ---创建视图布局---
- (void)createViewAbout
{
    // 创建点击的button
    self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 200, 120, 120)];
    _imageButton.layer.cornerRadius = 10;
    _imageButton.layer.borderColor = [[UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1.0] CGColor];
    _imageButton.layer.borderWidth = 1.0;
    [self.view addSubview:_imageButton];
    UILabel * imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 40)];
    imageLabel.text = @"上传图片";
    imageLabel.textAlignment = UITextAlignmentCenter;
    imageLabel.font = [UIFont fontWithName:@"Arial" size:18];
    imageLabel.textColor = [UIColor grayColor];
    [self.imageButton addSubview:imageLabel];

    // 给button添加事件
    [self.imageButton addTarget:self action:@selector(clickUpLoadImage:) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark ---点击上传图片---
- (void)clickUpLoadImage:(UIButton *)button
{
    NSLog(@"上传头像");
    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    // 从相册选取
    UIAlertAction * alBum = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        // 设置类型
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        // 是否允许编辑
        picker.allowsEditing = YES;
        // 设置代理
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }];

    // 从相机选取
    UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机拍摄" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * picker1 = [[UIImagePickerController alloc] init];
        // 设置类型为相机拍摄
        picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker1.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker1.sourceType];
        picker1.allowsEditing = YES;
        picker1.delegate = self;
        [self presentViewController:picker1 animated:YES completion:nil];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    // 添加到alertVC上
    [alertVC addAction:cancel];
    [alertVC addAction:alBum];
    [alertVC addAction:camera];
    // 显示alertVC
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark ----实现UIImagePickerControllerDelegate中的协议方法-----
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 选取完图片后关闭视图
    [self dismissViewControllerAnimated:YES completion:^{

        // 1、从相册或者相机中获取到的图片
        UIImage * loadImage = info[UIImagePickerControllerOriginalImage];
        // 获取上传图片网址
        NSString * url = [NSString stringWithFormat:@"这里填写上传图片网址"];
        // 利用 AFHTTPRequestOperationManager上传
        AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];

        [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *fileName = [formatter stringFromDate:[NSDate date]];

            [formData appendPartWithFileData:UIImageJPEGRepresentation(loadImage, 0.3)  name:@"imageFile" fileName:@"fileName.png" mimeType:@"image/png"];

        } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSLog(@"头像上传成功反馈== %@", responseObject);
            // 图片网址
            [self.imageButton setBackgroundImage:loadImage forState:UIControlStateNormal];
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            NSLog(@"错误信息=====%@", error);
        }];
    }];
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值