Search code examples
iosimagedownloadmbprogresshud

How to add MBProgressHUD while saving the image photolibrary IOS7


Hi in my application i have the image on UIImageView and i have button called download once user click the download button the image will save to the Photolibrary. Now i want to add the Progressbar for the download to know the user its downloading the image.

I have used the MBProgressHUD for the progress but not able work like i wanted. I want to show once user click the download button like its downloading message with progress once image got downloaded i want to show like image downloaded please tell me how to achieve this one i tired something like that its not working.

My MBProgressHUD code.

- (IBAction)down:(id)sender {

   HUD = [[MBProgressHUD alloc] initWithView:self.view];
   HUD.labelText = @"downloading...";

   HUD.mode = MBProgressHUDModeAnnularDeterminate;
   [self.view addSubview:HUD];

   [HUD showWhileExecuting:@selector(download) onTarget:self withObject:nil animated:YES];

   UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);

 }

- (void)download {
float progress = 0.0;

while (progress < 1.0) {
    progress += 0.01;
    HUD.progress = progress;
    usleep(50000);
  }
}

I have used the above code to achieve but its not working like i wanted please tell me how to achieve this one.

Thanks.


Solution

  • - (IBAction)down:(id)sender {
    
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode =MBProgressHUDModeAnnularDeterminate;
    
    NSString *strloadingText = [NSString stringWithFormat:@"Downloading...."];
    HUD.labelText = strloadingText;
    
    
    [HUD show:YES];
    
    [HUD showWhileExecuting:@selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];
    
    
    
    }
    
    
    - (void)doSomeFunkyStuff {
    float progress = 0.0;
    
    while (progress < 1.0) {
        progress += 0.01;
        HUD.progress = progress;
    
        usleep(5000000);
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
                      ^{
                           // process in background thread
                          NSString *strloadingText = [NSString stringWithFormat:@"completed"];
                          HUD.labelText = strloadingText;UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
    
    
    
    
                           [HUD hide:YES];
    
    
                       });
    
     }
    }