在社交网络上分享iOS图片

10

我的应用程序允许用户拍照并在保存之前添加覆盖层。

我想让用户使用能够处理图像的任何应用程序(例如:电子邮件、Facebook、Twitter等)分享他的图片,就像Android上的Intent一样。

我尝试使用UIDocumentController,但它不像正式的相册那样显示Facebook或Twitter。同时,在拍摄第二张照片后,它也会导致我的应用程序崩溃。

有没有简单的方法可以实现?我不想使用Facebook SDK等工具。

这是我在拍照后所做的:

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:
 ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

     if(!error){
         //Resize the picture and add the overlay
         UIImage *picture = [self imageFromSampleBuffer:imageSampleBuffer];
         //Custom code letting me save the picture in a specific album
         [self.library saveImage:picture toAlbum:@"myApp" metadata:metadata withCompletionBlock:^(NSError *error,NSURL* assetURL) {
             if (error!=nil) {
                 NSLog(@"Big error: %@", [error description]);
             } else {
                 NSLog(@"Image Saved");

                NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"tmp.jpg"];
                //Only way to use UIDocumentController is to save the file at a known location
                NSData* imagedata = UIImageJPEGRepresentation(picture, 0.9f);
                [imagedata writeToFile:path atomically:NO];
                NSLog(@"%@",path);
                docController.URL = [NSURL fileURLWithPath:path];
                 // This make my app crash after the second picture
                 [docController presentPreviewAnimated:YES];
            }
         }];

     } else {
         NSLog(@"%@",error);
     }

 }];
1个回答

13

iOS内置社交分享工具包。您可以通过电子邮件、Facebook和Twitter分享图像。但是,如果要使用Google+和其他社交服务,您需要其各自的SDK。

1)对于Facebook

SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [controller setInitialText:message];
    [controller addImage:image];
    [self presentViewController:controller animated:YES completion:Nil];

2) 对于 Twitter,请将 SLServiceTypeFacebook 替换为 SLServiceTypeTwitter。

3) 对于电子邮件

MFMailComposeViewController *emailShareController = [[MFMailComposeViewController alloc] init];
    emailShareController.mailComposeDelegate = self;
    [emailShareController setSubject:@"Share Image"];
    [emailShareController setMessageBody:message isHTML:NO];
    [emailShareController addAttachmentData:UIImageJPEGRepresentation(image, 1) mimeType:@"image/jpeg" fileName:@"your_image.jpeg"];
    if (emailShareController) [self presentViewController:emailShareController animated:YES completion:nil];

4) 记得将 Social.Framework 添加到您的项目中,并添加以下头文件

#import <MessageUI/MFMailComposeViewController.h>
#import <Social/Social.h>
#import <MobileCoreServices/MobileCoreServices.h>

5) 将您的视图控制器设置为

MFMailComposeViewControllerDelegate

一旦邮件发送,关闭MailViewController-

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

很棒,谢谢!没有预制的按钮让用户选择吗? - Oyashiro
它几乎完美地工作,但当我发送电子邮件时,它会发送它,但控制器永远不会被解除,是否有一种方法在成功后返回到上一个视图? - suMi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接