注:用的AFNetworking版本低于3.0
1呼胚、遵守協(xié)議UIImagePickerControllerDelegate,UINavigationControllerDelegate
2观蓄、首先創(chuàng)建按鈕,添加點(diǎn)擊事件
//點(diǎn)擊事件
- (void)onTapSecondCell
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self) weakSelf = self;
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"打開相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf takePhoto];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"打開相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf localPhoto];
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[alert dismissViewControllerAnimated:YES completion:^{
NSLog(@"取消準(zhǔn)備照片");
}];
}];
[alert addAction:action1];
[alert addAction:action2];
[alert addAction:action3];
[self presentViewController:alert animated:YES completion:nil];
}
//打開相機(jī)
- (void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])//相機(jī)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:^{
NSLog(@"調(diào)取相機(jī)成功");
}];
}
else{
NSLog(@"模擬器情況下無法打開");
}
}
//打開相冊
- (void)localPhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])//相冊
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:^{
NSLog(@"調(diào)取相冊成功");
}];
}
else{
NSLog(@"相冊打不開應(yīng)該是出問題了");
}
}
實(shí)現(xiàn)協(xié)議方法
//當(dāng)選擇一張圖片后進(jìn)入到這個協(xié)議方法里
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//當(dāng)選擇的類型是圖片
if ([type isEqualToString:@"public.image"])
{
//先把圖片轉(zhuǎn)成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data1;
if (UIImagePNGRepresentation(image) == nil)
{
data1 = UIImageJPEGRepresentation(image, 1);
}
else
{
data1 = UIImagePNGRepresentation(image);
}
//圖片保存的路徑
//這里將圖片放在沙盒的documents文件夾中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把剛剛圖片轉(zhuǎn)換的data對象拷貝至沙盒中 并保存為image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data1 attributes:nil];
//得到選擇后沙盒中圖片的完整路徑
NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
long long abc = [self fileSizeAtPath:filePath];
if (abc < 1024000) {
_headerImage = image;
}
else {
//如果圖片尺寸過大則壓縮
UIImage *small = [Help imageWithImageSimple:image scaledToSize:CGSizeMake(image.size.width/4, image.size.height/4)];
NSData *data;
if (UIImagePNGRepresentation(small) == nil)
{
data = UIImageJPEGRepresentation(small, 0.25);
}
else
{
data = UIImagePNGRepresentation(small);
}
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
_headerImage = small;
}
//關(guān)閉相冊界面
[picker dismissViewControllerAnimated:YES completion:^{
NSLog(@"關(guān)閉相冊界面");
}];
[self.tableView reloadData];
}
}
//判斷文件的大小
- (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
NSLog(@"您取消了選擇圖片222");
}];
}
選中圖片上傳
- (void)postData
{
AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
manage.responseSerializer = [AFHTTPResponseSerializer serializer];
__weak typeof(self) weakSelf = self;
[MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
[manage POST:Health_GetReportPic parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//上傳圖片
NSData *data = UIImageJPEGRepresentation(_headerImage, 1.0);
[formData appendPartWithFileData:data name:@"report_pic_path" fileName:@"image.png" mimeType:@"png"];
//name-->對應(yīng)數(shù)據(jù)庫存儲的字段
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
if ([dict[@"message"] isEqualToString:@"數(shù)據(jù)插入成功"])
{
[MBProgressHUD hideHUDForView:self.tableView animated:YES];
[weakSelf addAlertControllerWithMessage:@"上傳成功"];
//沙盒中圖片的完整路徑
NSString *string1 = [NSHomeDirectory() stringByAppendingString:@"/Documents/image.jpg"];
NSFileManager *fileManage = [NSFileManager defaultManager];
[fileManage removeItemAtPath:string1 error:nil];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error.localizedDescription);
[MBProgressHUD hideHUDForView:self.tableView animated:YES];
[weakSelf addAlertControllerWithMessage:@"上傳失敗"];
}];
}