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];
}