2013-11-23 92 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

嘗試過,但沒有奏效 –