標籤:dict 網路請求 session activity mutable alert 方式 nslog imageview
一、點擊頭像圖片 或者按鈕 在相簿選擇照片返回img,網路上傳頭像要用data表單上傳
(1)上傳頭像屬性
// 圖片二進位格式 表單上傳@property (nonatomic, strong) NSData *imageWithData;
(2)頭像點擊事件
- (void)headImageEvent{ NSLog(@"上傳頭像"); [self selectPhotoAlbumWithSelectPhotoHandle:^(UIImage *img) { self.headerImageView.image = img; NSData *dataWithImage; dataWithImage = UIImageJPEGRepresentation(img, 0.3); self.imageWithData = dataWithImage; }]; }
(3)開啟相簿或者拍照
/** 彈出提示框 選擇相機或者相簿 @param selectPhotoHandle 選中或拍攝的圖片 */- (void)selectPhotoAlbumWithSelectPhotoHandle:(void (^)(UIImage *))selectPhotoHandle{ self.selectPhotoHandle = selectPhotoHandle; UIAlertController *av = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self takePhoto]; }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"從手機相簿選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self LocalPhoto]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [av addAction:action1]; [av addAction:action2]; [av addAction:cancelAction]; [self presentViewController:av animated:YES completion:nil];}//開始拍照-(void)takePhoto{ UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //設定拍照後的圖片可被編輯 picker.allowsEditing = YES; picker.sourceType = sourceType; [self presentViewController:picker animated:YES completion:nil]; }else{ NSLog(@"類比其中無法開啟照相機,請在真機中使用"); }}//開啟本地相簿-(void)LocalPhoto{ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //設定選擇後的圖片可被編輯 picker.allowsEditing = YES; [self presentViewController:picker animated:YES completion:nil];}//當選擇一張圖片後進入這裡-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; if (self.selectPhotoHandle) { self.selectPhotoHandle(image); } [picker dismissViewControllerAnimated:YES completion:nil]; }
(4)網路上傳頭像
- (void)postUserHeaderImage{ NSMutableDictionary *parameter = [[NSMutableDictionary alloc]init]; [parameter setValue:userToken forKey:@"userId"]; [parameter setValue:self.nickeNameTextField.text forKey:@"nickName"]; [[[NetRequest alloc]init]postRequestWithAPINameForData:@"/robot/userController/updateRecord.do" parameters:parameter imageWithData:self.imageWithData imageWithNameFile:@"portrait" NetRequestProgress:nil NetRequestSuccess:^(NSInteger code, NSString *msg, id response) { ShowMessage(@"編輯資料成功"); } NetRequestFaile:^(NSInteger code, NSString *msg, id response) { ShowMessage(@"擷取網路資料失敗"); }];}
(5)上傳頭像方法
// 以流檔案方式上傳圖片(表單上傳)- (void)postRequestWithAPINameForData:(NSString *)api parameters:(NSDictionary *)param imageWithData:(NSData *)fileData imageWithNameFile:(NSString *)nameFile NetRequestProgress:(NetRequestProgress)progress NetRequestSuccess:(NetRequestSuccess)sucess NetRequestFaile:(NetRequestFaile)faile{ // 請求地址 NSString *urlString = [NSString stringWithFormat:@"%@/%@", baseUrl, api]; // 設定狀態列網路訪問的風火輪 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; // 網路請求Session AFHTTPSessionManager* session = [AFHTTPSessionManager manager]; session.requestSerializer.timeoutInterval = 15.f; session.responseSerializer.acceptableContentTypes = [session.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; // session.responseSerializer = [AFHTTPResponseSerializer serializer]; // 參數初始化 NSMutableDictionary *dicParam; if (param) { dicParam = [NSMutableDictionary dictionaryWithDictionary:param]; } else { dicParam = [NSMutableDictionary dictionary]; }// [dicParam setObject:@"i" forKey:@"p"]; // 平台// [dicParam setObject:@"1.0.0" forKey:@"v"]; // 版本 [session POST:urlString parameters:dicParam constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { if (fileData.length > 0) { NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; // 注意 img 是後台給的流檔案名稱 一定對應介面否則上傳失敗 [formData appendPartWithFileData:fileData name:nameFile fileName:[NSString stringWithFormat:@"%ld.jpg",(unsigned long)time] mimeType:@"image/jpeg"]; } } progress:^(NSProgress * _Nonnull uploadProgress) { if (progress) { progress(uploadProgress); } } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 取消設定狀態列風火輪 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; // API返回結果 NSInteger nCode = [[responseObject objectForKey:@"status"] integerValue]; NSString *strMsg = [responseObject objectForKey:@"msg"];// id res = [responseObject objectForKey:@"data"]; if (sucess) { sucess(nCode,strMsg,responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { // 取消設定狀態列風火輪 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding]; NSLog(@"----%@",errResponse); if (faile) { faile([@"99999" intValue],@"網路請求失敗\n請檢查網路設定",nil); } }];}
相簿選擇頭像或者拍照 上傳頭像以NSData 圖片二進位格式 表單上傳