上传图片要求:选择图片后先显示出来,当点击提交按钮时才可以上传图片。
先上单张图片上传代码;
需要用到一个第三方 `#import “BoPhotoPickerViewController.h”
#import "BoPhotoPickerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
需要遵循代理
HJMHuiFangController ()<BoPhotoPickerProtocol, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
但是运行会出现报错因为项目中还有两个类没有添加
添加完这两个类之后运行不会出现报错。就可以放心写代码了。
创建一个按钮,当点击按钮时会打开选择图片控制器,选择想要上传的图片,然后点击又上角确定,会返回到按钮页面,当点击上传按钮时,这个时候才会上传图片。具体代码如下。
self.selectPic = [[UIButton alloc]init];
self.selectPic.frame = CGRectMake(5, 10, 100, 100);
[self.selectPic setImage:[UIImage imageNamed:@"addphoto"] forState:UIControlStateNormal];
[self.selectPic addTarget:self action:@selector(picClick:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:self.selectPic];
这个是创建选择图片按钮
#pragma mark 选择图片
- (void)picClick:(UIButton *)button {
NSLog(@"2121221");
BoPhotoPickerViewController *picker = [[BoPhotoPickerViewController alloc]init];
picker.assetsFilter = [ALAssetsFilter allPhotos];
picker.showEmptyGroups = YES;
picker.delegate = self;
picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return YES;
}];
[self presentViewController:picker animated:YES completion:nil];
}
这是打开选择图片控制器方法
#pragma mark - BoPhotoPickerProtocol
- (void)photoPickerDidCancel:(BoPhotoPickerViewController *)picker {
NSLog(@"选择取消按钮");
[picker dismissViewControllerAnimated:YES completion:nil];
}
这是选择取消按钮方法
#pragma mark 上传图片代理方法
- (void)photoPicker:(BoPhotoPickerViewController *)picker didSelectAssets:(NSArray *)assets {
if (assets.count == 1) {
ALAsset *asset = assets[0];
self.picArray = assets[0];
[self.selectPic setImage:nil forState:UIControlStateNormal];
UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
[self.selectPic setImage:tempImg forState:UIControlStateNormal];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
这是选择确定按钮方法
- (void)photoPickerTapAction:(BoPhotoPickerViewController *)picker {
if(![self checkCameraAvailability]){
NSLog(@"没有访问相机权限");
return;
}
[picker dismissViewControllerAnimated:NO completion:nil];
UIImagePickerController *cameraUI = [UIImagePickerController new];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.cameraFlashMode=UIImagePickerControllerCameraFlashModeAuto;
[self presentViewController: cameraUI animated: YES completion:nil];
}
#pragma mark - UIImagePickerDelegate
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
if (!error) {
NSLog(@"保存到相册成功");
}else{
NSLog(@"保存到相册出错%@", error);
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage;
if (CFStringCompare((CFStringRef) mediaType,kUTTypeImage, 0)== kCFCompareEqualTo) {
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
}
[self.selectPic setImage:nil forState:UIControlStateNormal];
[self.selectPic setImage:originalImage forState:UIControlStateNormal];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- (BOOL)checkCameraAvailability {
BOOL status = NO;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized) {
status = YES;
} else if (authStatus == AVAuthorizationStatusDenied) {
status = NO;
} else if (authStatus == AVAuthorizationStatusRestricted) {
status = NO;
} else if (authStatus == AVAuthorizationStatusNotDetermined) {
status = NO;
}
return status;
}
必须实现的代理方法
ALAsset *asset = (ALAsset *)self.picArray;
UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
[manger POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(tempImg, 0.3) name:@"imageFile" fileName:@"fileName.png" mimeType:@"text/plain"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"宝宝头像上传== %@", responseObject[@"msg"]);
NSLog(@"宝宝头像上传== %@", responseObject);
if ([responseObject[@"success"] integerValue] == 1) {
// 返回到历史界面
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
[SVProgressHUD dismiss];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"错误信息=====%@", error);
}];
[self.dataArray removeAllObjects];
}
提交方法