UIPregressView 使用 AFNetWorking 上传图像
Posted
技术标签:
【中文标题】UIPregressView 使用 AFNetWorking 上传图像【英文标题】:UIPregressView Upload Image with AFNetWorking 【发布时间】:2014-05-26 06:59:44 【问题描述】:我是 ios 开发的新手。我使用“AFNetworking”创建上传图像方法。我想在处理时添加UIProgressView
。
现在我在上传方法中使用“DejalBezelActivityView”
“DejalBezelActivityView”可以使用,但我想使用UIProgressView
。
怎么做?
这是我的代码示例:
-(void) uploadImage
// Set Data to Web API
// NSMutableArray *newPictureData = ...
// WEB API
[PictureClient uploadPictures:newPictureData whenCompleted:^(BOOL success, NSString *data, NSError *error)
if (success)
[DejalBezelActivityView removeViewAnimated:YES];
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@“Complete"
message:@“Upload Cpmplete"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[myAlertView show];
// Do somethings
// ...
else
[DejalBezelActivityView removeViewAnimated:YES];
// Error
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@“Error"
message:errorCode
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
];
[DejalBezelActivityView activityViewForView:self.view withLabel:@“During Picture"];
【问题讨论】:
【参考方案1】:喜欢@StatusReport answered 和MBHudProgress
// 1. Create `AFHTTPRequestSerializer` which will create your request.
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
// 2. Create an `NSMutableURLRequest`.
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://www.myurl.com"
parameters:dataToPost
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:imageData
name:@"attachment"
fileName:@"myimage.jpg"
mimeType:@"image/jpeg"];
];
// 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject)
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"Success %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"Failure %@", error.description);
];
// 4. Set the progress block of the operation.
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite)
NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
];
// 5. Begin!
[operation start];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
【讨论】:
以上是关于UIPregressView 使用 AFNetWorking 上传图像的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 中“setImageWithURLRequest:placeholderImage:success:”的 Alamofire 方式
如何在 iOS 中使用 NSDictionary 添加图像并使用 POST 方法将数组列表发送到服务器