不積跬步,無以至千里
不積小流,無以成江海
項目中的圖片上傳肯定是必不可少的內容,以下先將項目中的copy下來,而且僅僅是作為頭像單個文件的上傳,以后有時間在詳細整理下
1. 點擊頭像按鈕
所在的視圖控制器需要遵從下<UIImagePickerControllerDelegate>代理
- (void)changeImage:(UIButton *)sender {
//創建常見的下滑提示欄, 注意iOS8 才有這個控件
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *fromPhotoAction = [UIAlertAction actionWithTitle:@"從相冊選擇" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
UIAlertAction *fromCameraAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
[alert addAction:cancelAction];
[alert addAction:fromCameraAction];
[alert addAction:fromPhotoAction];
[self presentViewController:alert animated:YES completion:nil];
}
2. 選中圖片的處理
代理方法獲取圖片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
//可裁剪
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//原圖
//UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}
3. 保存圖片
-(void)saveImage:(UIImage *)image{
//傳遞的參數
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@""forKey:@"userId"];
//圖片壓縮處理
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
//利用AFNetworking上傳data
AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[mgr POST:[NSString stringWithFormat:@"%@%@",BaseAPI,@"/app/uploadPhoto"] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//上傳時使用當前的系統事件作為文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
//上傳圖片
[formData appendPartWithFileData:imageData name:@"imgFile" fileName:fileName mimeType:@"image/jpeg"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//返回結果
NSLOG(@"Success >>>>>>>>>>>>>>>>>%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
圖片上傳的基本功能算是有了