只有
才能正常工作[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 :(得分:0)
我认为你应该尝试在主线程中运行它,如
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];
});