Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS sharing picture on Social Networks

Tags:

ios

image

sharing

My app let the user take a picture, and add an overlay before saving it.

I'd like to let the user share his picture using whatever app able to handle images (i.e : email, facebook, twitter...), like an Intent on Android.

I tried to use UIDocumentController, but it doesn't show Facebook or Twitter as it does in the official gallery. It also makes my app crashes after taking the second picture.

Is there a simple way to do so ? I don't wan't to use the Facebook SDK and so on.

Here is what I do when the picture is taken :

[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);
     }

 }];
like image 904
Oyashiro Avatar asked Jul 11 '13 13:07

Oyashiro


1 Answers

iOS has an inbuilt social sharing kit. You can share images via Email, Facebook and Twitter. But for using Google+ and other social services you will need their respective SDKs.

1) For Facebook

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

2) For twitter replace SLServiceTypeFacebook with SLServiceTypeTwitter.

3) For Email

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) Remember to add Social.Framework to your project and the following header files

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

5) Set your view controller as delegate of

MFMailComposeViewControllerDelegate

Dismiss MailViewController once mail is send-

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
like image 195
vforvendetta Avatar answered Oct 21 '22 00:10

vforvendetta