iOS在社交网络上分享图片

我的应用程序让用户拍摄照片,并在保存前添加叠加层.

我想让用户使用任何能够处理图像的应用程序(即电子邮件,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);
     }

 }];

解决方法

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];
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...