UIPregressView使用AFNetWorking上传图像

时间:2014-05-26 06:59:45

标签: ios objective-c afnetworking uiprogressview

我是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 个答案:

答案 0 :(得分:1)

喜欢带有answered

的@StatusReport 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];