Ios开发,图片上传

首先接受

UIImagePickerControllerDelegate,UINavigationControllerDelegate 协议



加载方法

- (void)shangchuanImage{

    UIActionSheet *sheet;

    // 判断是否支持相机

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];

    }

    else {

        

        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];

    }

    [sheet showInView:self.view];

}


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

    

    NSUInteger sourceType = 0;

    

    // 判断是否支持相机

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        

        switch (buttonIndex) {

            case 2:

                // 取消

                return;

            case 0:

                // 相机

                sourceType = UIImagePickerControllerSourceTypeCamera;

                break;

                

            case 1:

                // 相册

                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                break;

        }

    }

    else {

        if (buttonIndex == 1) {

            

            return;

        } else {

            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        }

    }

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

    imagePickController.delegate = self;

    imagePickController.allowsEditing = YES;

    if (sourceType ==UIImagePickerControllerSourceTypeCamera) {

        imagePickController.sourceType =  UIImagePickerControllerSourceTypeCamera;

    }else if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary){

        imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    }else if(sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum){

        imagePickController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    }

    [self presentViewController:imagePickController animated:YES completion:nil];

}




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

    

    [self saveImage:info[UIImagePickerControllerOriginalImage] WithName:@"userAvatar"];

    

    //处理完毕,回到个人信息页面

    [picker dismissViewControllerAnimated:YES completion:NULL];

    

    

}


//保存图片

- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName

{

    NSData* imageData = UIImageJPEGRepresentation(tempImage, 0.001);

    //    UIImageJPEGRepresentation(currentImage, 1);

    NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString* totalPath = [documentPath stringByAppendingPathComponent:imageName];

    

    //保存到 document

    [imageData writeToFile:totalPath atomically:NO];

    

    //保存到 NSUserDefaults

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setObject:totalPath forKey:@"avatar"];

    

    self.imageName = imageName;

    self.data = imageData;

    

    UIImage *savedImage = [[UIImage alloc]initWithContentsOfFile:totalPath];

    

    [self.cell1.shangchuan setBackgroundImage:savedImage forState:UIControlStateNormal];

    [self.cell1.shangchuan setTitle:@"" forState:UIControlStateNormal];

    

    //图片上传

    [self upload:@"imgFile" filename:self.imageName mimeType:@"image/jpeg" data:self.data];

}


//图片上传的方法

- (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data

{

    

    

    [SVProgressHUD showInfoWithStatus:@"正在提交请稍后"];

    //

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.securityPolicy = [self customSecurityPolicy];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSString * url = [NSString stringWithFormat:@"%@/upfile",BASE_URL];

    

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

        // 上传图片,以文件流的格式

        [formData appendPartWithFileData:data name:name fileName:filename mimeType:mimeType];

        

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

       id dicu = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];


        if ([[dicu objectForKey:@"rcd"] isEqualToString:@"R0001"]) {

            self.urlIcon = [dicu objectForKey:@"url"];

        }

      

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

        NSLog(@"%@",error);

    }];

    

    [operation start];

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值