如何暂停主线程直到块完成Objective-C

时间:2015-08-20 15:56:26

标签: ios objective-c multithreading parse-platform

我有一个问题。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissViewControllerAnimated:YES completion:^{
    if (image == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try again !" message:@"Please try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
    }else{
        NSData *fileData;
        NSString *fileName;
        NSString *fileType;
        fileData = UIImagePNGRepresentation(image);
        fileName = @"profilePic.png";
        fileType = @"Image";
        PFFile *file = [PFFile fileWithName:fileName data:fileData];
        [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                    message:[NSString stringWithFormat:@"Please try sending your message again. %@",error.userInfo]
                                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }else{
                [[PFUser currentUser]setObject:file forKey:@"profilePic"];
                [[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeed, NSError* error){
                    if (!error) {
                        NSLog(@"New profile pic loaded.");
                        [self.activity stopAnimating];
                        [self.reloadPicBtn setHidden:NO];
                    }else{
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try again !" message:@"Please try again. Failed to load your new profile pic :(" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                        [alert show];

                    }
                }];
            }
        }];
    }
}];

}

嗯,显示reloadPicBtn,但是如果我调用一个重新加载图片的函数,它就会失败。当我试图理解WTF正在使用NSLog时,它会记录一个字符串。所以代码已被执行。但屏幕上没有结果。该函数与viewDidLoad中调用的函数相同。但是当它从viewDidLoad调用时它会起作用。当它被再次调用时,没有结果。

重新加载功能:

- (void) loadProfilePic{
PFQuery* query = [PFUser query];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject* object, NSError* error){
    if(!error){
        PFFile* imageFile = object[@"profilePic"];
        [imageFile getDataInBackgroundWithBlock:^(NSData* data, NSError* error){
            if (!error) {
                self.activity.hidden = NO;
                [self.activity startAnimating];
                self.profilePic.image = [UIImage imageWithData:data];
                NSLog(@"Profile pic shown");
            }
            else{
                NSLog(@"Error 2: %@",error);
            }
        }];

    }else{
        self.profilePic.image = [UIImage imageNamed:@"profile@2.png"];
        NSLog(@"Fail 1 : %@",error);
    }
}];
}

请帮帮我。

打印出显示的个人资料照片。但它甚至不使用网络下载图像。

0 个答案:

没有答案