dispatch_async方法实现第二个方法而不执行第一个方法

时间:2018-04-11 04:58:59

标签: ios objective-c uiactivityviewcontroller dispatch-async

NSString *firstID = @"https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";

想象一下,当我传递第一个id时,我的firstID是缩略图,它会获得完整的图像并获得原始图像

    dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"0");
                [self GetFullImage:firstID];
                NSLog(@"1");
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"2");
                    UIActivityViewController *activityViewController = [[UIActivityViewController alloc]                                                                initWithActivityItems:PAN applicationActivities:nil];
                    activityViewController.popoverPresentationController.sourceView = self.view;
                    [self presentViewController:activityViewController animated:YES completion:nil];
                    NSLog(@"3");
                    NSLog(@"PAGGGGGG:%@",PAN);

                });
            });

第一个方法代码:

- (void)GetFullImage:(NSString *)FullPath{
    NSString *UserName = [Appdata sharedDataModel].userNameString;
    NSString *Password = [Appdata sharedDataModel].passwordString;
    NSString *DeviceType = [Appdata device];
    NSString *VersionNo = [Appdata buildVersion];

    NSMutableDictionary *parameterDict = [[NSMutableDictionary alloc] init];

    [parameterDict setObject:UserName forKey:@"UserName"];
    [parameterDict setObject:Password forKey:@"Password"];
    [parameterDict setObject:DeviceType forKey:@"DeviceType"];
    [parameterDict setObject:VersionNo forKey:@"VersionNo"];
    [parameterDict setObject:FullPath forKey:@"FullPath"];

    [[APIUtility sharedInstance] get_Orig_Full_Image_Disp:parameterDict finishResult:^(id response) {

        if(response){
            @try {
                iconImage=nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SHaREDATA removeAllObjects];
                    [SHaREDATA addObject:response];
                    NSLog(@"15975368420:%@",SHaREDATA);

                    NSString *FullImage = [[[SHaREDATA objectAtIndex:0]valueForKey:@"Path"]objectAtIndex:0];
                    NSLog(@"ppppppppppp:%@",FullImage);
                    NSData* data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:FullImage]];
                    iconImage = [UIImage imageWithData:data];
                    NSLog(@"YYYYYYYYYY:%@",iconImage);
                    NSString  * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.jpg"];
                    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
                    PAN =@[iconImage];
                });

            }
            @catch (NSException *exception) {
            }
        }
    }];
}

结果(我得到):

2018-04-11 10:03:56.105 search[2119:29318] 0
2018-04-11 10:03:56.107 search[2119:29318] 1
2018-04-11 10:03:56.108 search[2119:29318] 2
2018-04-11 10:03:56.206 search[2119:29318] 3
2018-04-11 10:03:56.206 search[2119:29318] PAGGGGGG:(null)
2018-04-11 10:10:47.299 search[2208:32130] returnDictionary = (
    {
    Caption = "";
    Path = "https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";
    ReturnValue = 0;
    }
)
2018-04-11 10:10:47.299 search[2208:32130] 15975368420:(
    (
       {
        Caption = "";
        Path = "https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg";
        ReturnValue = 0;
       }
    )
 )
 2018-04-11 10:10:47.300 search[2208:32130] 
 ppppppppppp:https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg
 2018-04-11 10:10:49.187 search[2208:32130] YYYYYYYYYY:<UIImage:0x7c815d70>, {960, 720}

我尝试了很多方法,但是所有执行都像我的结果我想先执行完整图像而不是执行UIActivityController请帮助我在这很多天里坚持。

当我使用dispatch_sync时阻塞方法,这是在这个方法中。

1 个答案:

答案 0 :(得分:1)

在我看来,在这种情况下你应该使用块。使用completionBlock重新制作GetFullImage:方法并在completionBlock中显示UIActivityController。您可以查看以下代码

- (void)GetFullImage:(NSString *)FullPath completion:(void(^)(UIImage* image))completion{
  NSString *UserName = [Appdata sharedDataModel].userNameString;
  NSString *Password = [Appdata sharedDataModel].passwordString;
  NSString *DeviceType = [Appdata device];
  NSString *VersionNo = [Appdata buildVersion];

  NSMutableDictionary *parameterDict = [[NSMutableDictionary alloc] init];

  [parameterDict setObject:UserName forKey:@"UserName"];
  [parameterDict setObject:Password forKey:@"Password"];
  [parameterDict setObject:DeviceType forKey:@"DeviceType"];
  [parameterDict setObject:VersionNo forKey:@"VersionNo"];
  [parameterDict setObject:FullPath forKey:@"FullPath"];

  [[APIUtility sharedInstance] get_Orig_Full_Image_Disp:parameterDict finishResult:^(id response) {

    if(response){
      @try {
        iconImage=nil;
        dispatch_async(dispatch_get_main_queue(), ^{
          [SHaREDATA removeAllObjects];
          [SHaREDATA addObject:response];
          NSLog(@"15975368420:%@",SHaREDATA);

          NSString *FullImage = [[[SHaREDATA objectAtIndex:0]valueForKey:@"Path"]objectAtIndex:0];
          NSLog(@"ppppppppppp:%@",FullImage);
          NSData* data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:FullImage]];
          iconImage = [UIImage imageWithData:data];
          NSLog(@"YYYYYYYYYY:%@",iconImage);
          NSString  * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.jpg"];
          [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
          PAN =@[iconImage];

          if (completion) {
            completion(iconImage);
          }
        });

      }
      @catch (NSException *exception) {
        if (completion) {
          completion(nil);
        }
      }
    }
  }];
}

用法:

[self GetFullImage:firstID completion:^(UIImage *image) {
  if (!image) {
    return;
  }

  NSLog(@"2");
  UIActivityViewController *activityViewController = [[UIActivityViewController alloc]                                                                initWithActivityItems:@[iconImage] applicationActivities:nil];
  activityViewController.popoverPresentationController.sourceView = self.view;
  [self presentViewController:activityViewController animated:YES completion:nil];
  NSLog(@"3");
  NSLog(@"PAGGGGGG:%@",image);
}];