如何从网址加载图片并分享该图片?

时间:2017-03-25 09:30:39

标签: ios objective-c uiactivityviewcontroller

我想使用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)

请帮帮我。

2 个答案:

答案 0 :(得分:1)

首先,您需要准备所有网址并预取图片

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

使用回调,然后您可以将下载的图像放入共享控制器

答案 1 :(得分:1)

你可以使用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];
    }

     }