简体   繁体   中英

MBProgressHUD doesn't appear

It just works as expected when there is only

[HUD setMode:MBProgressHUDModeIndeterminate];
[HUD setLabelText:@"Uploading"];
[HUD show:YES];

But when I append these below code to the above code HUD doesn't appear until HUD's mode goes into determinate.

//turning UIImageView with UILabel on it into UIImage
NSData *jpegData = UIImageJPEGRepresentation([self flattenImageView:imageView withLabel:label], 0.1);
NSLog(@"file size:%fk", [jpegData length]/1024.0f);
NSString *UUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSDictionary *params = @{@"username": USER_DEFAULTS_USERNAME,
                         @"device_id" : UUID};

//preparing AFNetworking for file upload
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:REMOTE_SERVER]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"actions/mobileAction.php" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:jpegData name:@"picture_file" fileName:@"wedding_photo.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    HUD.mode = MBProgressHUDModeText;
    [HUD setLabelText:@"Success"];
    [self performSelector:@selector(hideHUDAndBack) withObject:Nil afterDelay:0.2];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    HUD.mode = MBProgressHUDModeText;
    [HUD setLabelText:@"Failed"];
    [self performSelector:@selector(hideHUDAndBack) withObject:Nil afterDelay:0.2];
}];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    HUD.progress = (double )totalBytesWritten/(double )totalBytesExpectedToWrite;
}];

//making the HUD go into determinate mode.
HUD.mode = MBProgressHUDModeDeterminate;
HUD.progress = 0.0f;
[client enqueueHTTPRequestOperation:operation];

I think the thing that causing this behavior is this method

[self flattenImageView:imageView withLabel:label]

its body is

- (UIImage *)flattenImageView:(UIImageView *)aImageView withLabel:(UILabel *)aLabel{
    UILabel *originalLabel = aLabel;
    UIImageView *originalImageView = aImageView;

    CGSize originalSize = originalImageView.frame.size;
    CGFloat originalFontSize = originalLabel.font.pointSize;

    CGSize imageSize = originalImageView.image.size;

    CGFloat w_ratio = originalSize.width / imageSize.width;
    CGFloat h_ratio = originalSize.height / imageSize.height;

    CGFloat new_center_x = label.center.x / w_ratio;
    CGFloat new_center_y = label.center.y / h_ratio;
    CGFloat new_w = label.frame.size.width / w_ratio;
    CGFloat new_h = label.frame.size.height / h_ratio;

    UILabel *new_label = [self deepCopyUILabel:label];
    new_label.frame = CGRectMake(0, 0, new_w, new_h);
    new_label.center = CGPointMake(new_center_x, new_center_y);
    new_label.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:originalFontSize / w_ratio];
    [new_label sizeToFit];


    UIImageView *bigIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    bigIV.image = originalImageView.image;
    [bigIV addSubview:new_label];

    UIGraphicsBeginImageContextWithOptions(bigIV.frame.size, NO, 0.0);
    [bigIV.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imageToSave = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageToSave;
}

i think you should tru to run this in main thread like

dispatch_async(dispatch_get_main_queue(), ^{
    if(!HUD) HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.settingTable addSubview:HUD];
    HUD.delegate = self;
    HUD.userInteractionEnabled = NO;
    HUD.labelText = @"Updating user info...";
    [HUD show:YES];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM