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);
}
}];
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];
}
Related
Please Help us..
We want to share projects (i.e. Its Images, text and also my app url).
But i can't get SDK to share all on whatsApp, facebook, twitter, mails and messages. same as android developer using Intent called ACTION_SEND
Try this -
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Call shareText like this -
[self shareText:#"Your text" andImage:nil andUrl:nil];
When i share a video using the UIActivityViewController, the twitter post does not show the video. However, if it take the same video and post it using the twitter app, the video shows and plays embedded in the post.
I am using this code:
MyActivityItemProvider * videoItem = [[MyActivityItemProvider alloc] initWithPlaceholderItem:#""];
NSArray * activityItems = [NSArray arrayWithObjects:
videoItem,
nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
And in MyActivityItemProvider, I have this:
#implementation MyActivityItemProvider
- (MyActivityItemProvider * )initWithPlaceholderItem:(NSString *)placeholderItem
{
self = [super initWithPlaceholderItem:placeholderItem];
return self;
}
- (id)item
{
NSString * path = [[NSBundle mainBundle] pathForResource:#"video_name" ofType:#"mp4"];
NSURL * fileURL = [NSURL fileURLWithPath:path];
return fileURL;
}
#end
Is it possible for a twitter video post to have the video embedded in it (as if I posted it using the twitter app) when posting using the UIActivityViewController?? Any suggestions on how to achieve this (what it looks) rather simple task?
The video is .mp4 and it is 3.1 MB (like I said, appears to post just fine using the Twitter app and I can send the video via txt message fine).
Video cannot be shared with UIActivityViewController.
Thanks to the uploading media new API, you can share video to twitter by it. And I have tried it, it works.
Please check this: https://github.com/liu044100/SocialVideoHelper
You just need to call this class method.
+(void)uploadTwitterVideo:(NSData*)videoData comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion;
Try this :
+ (void)shareOnTwiiterFromView:(UIViewController *)vc userTitle:(NSString *)title shareURL:(NSString *)urlString
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:
SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result)
{
switch(result)
{
case SLComposeViewControllerResultCancelled:
break;
case SLComposeViewControllerResultDone:
break;
}
};
NSString *twTitle = [NSString stringWithFormat:#"%# %#",title,#"Your App Name"];
[tweetSheet setInitialText:twTitle];
if (![tweetSheet addURL:[NSURL URLWithString:urlString]])
{
NSLog(#"Unable to add the URL!");
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIPresentationController *control = tweetSheet.presentationController;
[tweetSheet setModalPresentationStyle:UIModalPresentationFormSheet];
[vc presentViewController:control.presentedViewController animated:YES completion:^
{
NSLog(#"Tweet sheet has been presented.");
}];
}
else
{
[vc presentViewController:tweetSheet animated:NO completion:^
{
NSLog(#"Tweet sheet has been presented.");
}];
}
}
else
{
[APP_DEL showErrorAlert:#"No Twitter Account" description:#"There are no twitter accounts configured. You can add or create a Twitter account in Settings."];
}
}
I'm developing an app where user can record video and save it in the library/or custom album. I'm able to create the custom album and record the video and save it in the default photo library i.e., camera roll.. But i'm unable to save it to the custom album. and other thing is the saved videos in the album must be showed in the collection view of the app i.e., like the gallery view.. so that user can able to click on the videos for the play.. it might be in the grid view/ collection view or might be in the table view where the UIImage should should display the videos saved in the album..
Here's code section which i used in the app development for creating the custom album.
ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder addAssetsGroupAlbumWithName:#"HEP" resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success");
} failureBlock:^(NSError *error)
{
NSLog(#"Error: Adding on Folder");
}];
For recording the video in the app.
- (IBAction)StartRecord:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
For saving it in the album i.e., default photo album not the custom album i created.
-(BOOL)startMediaBrowserFromViewController:(UIViewController*)controller usingDelegate:(id )delegate {
// 1 - Validations
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
// 2 - Get image picker
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = YES;
mediaUI.delegate = delegate;
// 3 - Display image picker
[controller presentModalViewController:mediaUI animated:YES];
return YES;
}
Since i'm beginner in iOS still learning.. I'm searching since for a month now. So please help me.
First create your album folder with below code & make sure this code paste in AppDelegate.m file didFinishLaunchingWithOptions method :
ALAssetsLibrary* libraryFolderSozialConnectVideo = [[ALAssetsLibrary alloc] init];
[libraryFolderSozialConnectVideo addAssetsGroupAlbumWithName:#"HEP" resultBlock:^(ALAssetsGroup *group)
{
NSLog(#"HEP Folder Created");
} failureBlock:^(NSError *error) {
NSLog(#"Error: Adding on Folder");
}];
Now, use below function for download your video in HEP folder :
- (void)downloadVideo:(NSURL *)videoURL {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSData *urlData = [NSData dataWithContentsOfURL:videoURL];
if (urlData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"thefile.mp4"];
[urlData writeToFile:filePath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.library saveVideo:[NSURL fileURLWithPath:filePath] toAlbum:#"HEP" completion:^(NSURL *assetURL, NSError *error)
{
NSLog(#"Success downloaded");
} failure:^(NSError *error) {
NSLog(#"Error : %#", [error localizedDescription]);
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.navigationController popViewControllerAnimated:YES];
}];
});
}
}); }
for videoURL in your didFinishPickingMediaWithInfo method call above function with below code :
self.library = [[ALAssetsLibrary alloc] init];
[self downloadVideo:[info valueForKey:UIImagePickerControllerMediaURL]];
I hope this works . . .
I know how to save a picture and this is the code until now. This code shows that the user taps a button and is able to take a photo.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];
//Save Your Image ======
UIImage *yourImage = imageview.image;//imageView.image;
NSString *docDirPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [docDirPath stringByAppendingPathComponent:#"myImageFile.png"];
[UIImagePNGRepresentation(yourImage) writeToFile:filePath atomically:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
However I want to sort my saved pictures into 6 Category or Genres which the user has to tap using the segment view control. Is this possible to do? I am very new in ios programming and objective-c in xcode and confused with many view controllers however I really like this to work and I a cannot find any resource about this topic. Thanks.
Here is a good demo for saving images to particular album using ALAssetsLibrary:
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
I hope it will help you out...
or
You can create different libraries using this piece of code:
-(void)createDirectory:(NSString *)directoryName atFilePath:(NSString *)filePath
{
NSString *filePathAndDirectory = [filePath stringByAppendingPathComponent:directoryName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(#"Create directory error: %#", error);
}
}
And fetch the images through their respective paths!!!
For more information see this: http://kmithi.blogspot.in/2012/08/ios-application-directory-structure.html
I'm using the following method to share an image and a url to the user's Facebook wall. The problem is that because I'm adding an image with the post it ends up being saved in a Facebook photo album. Perhaps this is the default behaviour, although I swear at one point it was posting just to the wall with a photo and not to a photo album.
thanks for any help
- (IBAction)shareAction:(id)sender {
SLComposeViewController * fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbController setInitialText:#"App Name"];
NSIndexPath* indexPath = [tableView indexPathForCell:sideSwipeCell];
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// output id of selected deal
#ifdef DEBUG
NSLog(#"deal id for selected row is %#", [[displayItems objectAtIndex:indexPath.row] objectForKey:#"id"]);
#endif
// display the youdeal deal image
link = [[displayItems objectAtIndex:indexPath.row] objectForKey:#"link"];
// download the youdeal deal image for FB posting
NSURL *url = [NSURL URLWithString:[[displayItems objectAtIndex:indexPath.row] objectForKey:#"image"]];
NSLog(#"url to image share %#", url);
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSLog(#"data to image share %#", data);
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
NSLog(#"tmpImage to image share %#", tmpImage);
ydImage = tmpImage;
NSLog(#"ydimageview.image %#", ydImage);
// [NSThread detachNewThreadSelector:#selector(downloadAndLoadImage) toTarget:self withObject:nil];
[fbController setInitialText:#"Check out this link."];
[fbController addURL:[NSURL URLWithString:link]];
[fbController addImage:ydImage];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
#ifdef DEBUG
NSLog(#"User Cancelled.");
#endif
[self removeSideSwipeView:YES];
}
break;
case SLComposeViewControllerResultDone:
{
#ifdef DEBUG
NSLog(#"Posted");
#endif
[self removeSideSwipeView:YES];
CustomAlertView *fbAlert = [[CustomAlertView alloc] initWithTitle:#"App Name" message:#"Facebook Post Successful!" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[fbAlert show];
}
break;
}};
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
looks like it might be an FB bug. sometimes they save to the albums sometimes not Unable to post image into facebook using SLComposeViewController?