使用AFN上傳圖片,顯示上傳進度
阿新 • • 發佈:2019-02-02
AFHTTPRequestOperation* uploadOperation = [mgr POST:url parameters:paramDict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData* imageData = UIImageJPEGRepresentation(image, 1);
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString* str = [formatter stringFromDate:[NSDate date]];
NSString* fileName = [NSString stringWithFormat:@"%@.jpeg", str];
[formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation* _Nonnull operation, id _Nonnull responseObject) {
if (successBlock) {
successBlock(operation, responseObject);
}
}
failure:^(AFHTTPRequestOperation* _Nonnull operation, NSError* _Nonnull error) {
if (errorBlock) {
errorBlock(operation, error);
}
}];
[uploadOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
CGFloat progress = ((float)totalBytesWritten) / totalBytesExpectedToWrite;
[SVProgressHUD showProgress:progress status:@"上傳中" maskType:SVProgressHUDMaskTypeBlack];
}];