如何从 url 加载图像并分享该图像?

how to load image from url and share that image?

我想使用 UIActivityViewController 分享图片。 这是我的代码。

- (IBAction)shareBUttonClick:(id)sender
{
   for(int i=0; i<[self.imagesDataArray count];i++)
  {
     [imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesDataArray objectAtIndex:i]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];  
     NSArray *postItems=@[imageView];
    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];

 //if iPhone
 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
    [self presentViewController:controller animated:YES completion:nil];
    }
//if iPad
else 
    {
    // Change Rect to position Popover
    UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:controller];
    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
 }
}

但是,它给了我 null(UIActivityViewController open in real device but sharing not there)

请帮助我。

首先您需要准备所有的 URL 和预取图片

    NSMutableArray * urls = [NSMutableArray   arrayWithCapacity:things.count];
    for(int i=0; i<[self.imagesDataArray count];i++) {
        [urls addObject:[NSURL URLWithString:   self.imagesDataArray[i]]];
    }
    [SDWebImagePrefetcher sharedImagePrefetcher]

有回调,就可以把下载的图片放到share controller中

您可以使用 sdwebimage 异步下载器,然后调用上传功能:

 [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

                        if (image && finished) {
    // now upload   
                              [self sendImageToShare:(UIImage *)image];
                        }
                    }]; 


    /*********************/


    - (void)sendImageToShare:(UIImage *)yourImage {
        if (yourImage) {
        NSMutableArray *activityItems= [NSMutableArray arrayWithObjects:yourImage, nil];
        UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

        [self presentViewController:activityViewController animated:YES completion:nil];
    }

     }