2013-11-23 60 views
0

它只是按预期工作只有MBProgressHUD不会出现

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

时可是当我附上下面这些代码上面的代码HUD没有出现,直到HUD的模式进入确定的。

//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]; 

我想事情,导致出现此问题是这种方法

[self flattenImageView:imageView withLabel:label] 

它的身体是

- (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; 
} 
+0

你是否在那里做任何导航,如移动到其他视图等。 – wasim

+0

不,我只是将uiimageview及其子视图合并到一个图像中 –

+0

您正在异步块中更新它。你需要在主线程上调用它来更新UI元素? – CW0007007

回答

0

我想你应该TRU在主线程像

运行此
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]; 
}); 
+0

尝试过,但没有奏效 –