一般開(kāi)發(fā)上傳用戶頭像使用AF框架就可以實(shí)現(xiàn)代碼如下,有需要的可以參考下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];? ? [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];? ? manager.requestSerializer.timeoutInterval = 8.f;?
? [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];?
?? [KVNProgress showWithStatus:@"正在上傳..." onView:self.view]; ?// 隨意用一個(gè)提示的彈框都可以 ?
?[manager POST:@“你的API接口” parameters:@“你的其余參數(shù)” constructingBodyWithBlock:^(id_Nonnull formData) {
[formData appendPartWithFileData:@“你的圖片轉(zhuǎn)為二進(jìn)制”name:@"file" fileName:@“可以隨意寫(xiě)一個(gè)文件名” mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id? _Nonnull responseObject) {
[KVNProgress showSuccessWithStatus:@"更換成功" onView:self.view];
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
[KVNProgress showErrorWithStatus:@"網(wǎng)絡(luò)異常請(qǐng)稍后再試" onView:self.view];
} ];
很簡(jiǎn)單徐矩,要是上傳多張可以寫(xiě)一個(gè)循環(huán)上傳就OK ,fileName 就用循環(huán)的次數(shù)來(lái)命名避免重復(fù)! 如有問(wèn)題或者更好的建議歡迎留言,謝謝诞丽!