Share an Image and Text on WhatsApp in IOS programmatically? - ios

I am to add an image and some text in whatsapp with the help of coding. How can I perform it to solve my problem?

You can post Image or Text on WhatsApp. But you wont post both at a time becouse whatsapp not provide any API that you can add caption and post image with text.
Following is an example for posting image from iPhone app to whatsapp as par WhatsApp Documentation:
in ViewController.h file:
#interface ViewController : UIViewController
{
}
#property(nonatomic,retain) UIDocumentInteractionController *documentationInteractionController;
in ViewController.m file:
- (IBAction)bocClick:(UIButton *)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"]; //here i am fetched image path from document directory and convert it in to URL and use bellow
NSURL *imageFileURL =[NSURL fileURLWithPath:getImagePath];
NSLog(#"imag %#",imageFileURL);
self.documentationInteractionController.delegate = self;
self.documentationInteractionController.UTI = #"net.whatsapp.image";
self.documentationInteractionController = [self setupControllerWithURL:imageFileURL usingDelegate:self];
[self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL
usingDelegate: (id ) interactionDelegate {
self.documentationInteractionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
self.documentationInteractionController.delegate = interactionDelegate;
return self.documentationInteractionController;
}

Related

Show "Open in" for MS Word, Excel, PPT in ios objective c

Is there any way I can show Open With or Open In window for ms office files I have seen UIDocumentInteractionController and QuickLook Framework but didn't work me. Does these two really support office files?
Quicklook must work
{
QLPreviewController *previewer = [[QLPreviewController alloc] init];
previewer.dataSource = datasource;
previewer.currentPreviewItemIndex = 0;
NSURL *urlFilePath = [[NSBundle mainBundle] URLForResource:#"name" withExtension:#"pdf"];
[self presentViewController:previewer animated:YES completion:nil];
}
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return urlFilePath;
}
where urlFilePath is file path url.
First of all Add "QuickLook.framework" to your project.
Its part of the iOS SDK frameworks.
Get the names of all files present in document directory in listOfFilesPresentInDocumentDirectory NSArray which is defined as the class variable.
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
listOfFilesPresentInDocumentDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:NULL];
Create an object of QLPreviewController, set self as its datasource, set its currentPreviewItemIndex and push it to the UINavigationController
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.currentPreviewItemIndex = indexPath.row;
[[self navigationController] pushViewController:previewController animated:YES];
Implement QLPreviewControllerDataSource protocol in you class’s definition and add following two QLPreviewControllerDataSource functions in the implementation block of your class
pragma mark - QLPreviewControllerDataSource Methods
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
return [listOfFilesPresentInDocumentDirectory count];
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSURL *fileURL = nil;
NSString *fileName = [listOfFilesPresentInDocumentDirectory objectAtIndex:index];
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString *previewFileFullPath = [documentDirectory stringByAppendingPathComponent:fileName];
fileURL = [NSURL fileURLWithPath:previewFileFullPath];
return fileURL;
}

Using iCarousel with images in folder

I have a folder (Documents/Images) which I use to store images downloaded from online, when the view with iCarsousel is launched infomation on which images to use is also sent so only certain images from the folder will be used.
However for some reason the below code does not seem to work and a blank view is shown and no error message given.
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
[carousel setType:iCarouselTypeCylinder];
[self getImages];
return [images count];
}
- (void)getImages{
images=[[NSMutableArray alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:databaseName];
db = [FMDatabase databaseWithPath:writableDBPath];
if (![db open]) {
return;
}
NSLog(#"getting images");
NSLog(_galleryid);
FMResultSet *result = [db executeQuery:#"SELECT * FROM mediaImages WHERE galleryID = ?;", _galleryid];
while ([result next]){
NSString *filename = [result stringForColumnIndex:1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
//configure carousel
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:#"Images"];
NSString *filepath = [fPath stringByAppendingString:#"/"];
filepath = [filepath stringByAppendingString:filename];
NSLog(filepath);
[images addObject:filepath];
}
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
view = [[UIView alloc] init];
view.contentMode = UIViewContentModeScaleAspectFit;
CGRect rec = view.frame;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
rec.size.width = 250;
rec.size.height = 250;
}
view.frame = rec;
UIImageView *iv;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
iv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
}
NSString *temp=[images objectAtIndex:index];
iv.image=[UIImage imageNamed:temp];
iv.contentMode = UIViewContentModeScaleAspectFit;
[view addSubview:iv];
return view;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
NSLog(#"Image is selected.");
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
switch (option)
{
case iCarouselOptionWrap:
{
return YES;
}
default:
{
return value;
}
}
}
Is there anything visably wrong with the code, or perhaps a good way to debug what the issue is?
My guess is that the problem is here:
iv.image=[UIImage imageNamed:temp];
temp is a path to a file, not the name of an image, so this won't work. Perhaps you want to use + imageWithContentsOfFile: instead?
If that's not the only problem, I highly recommend trying to isolate it further yourself with logging or debugging; for example if you had logged the value of temp and the image you get back from imageNamed:, you probably would have seen this problem as well.

Image not loading correctly in app

My app lets the user select the photo from camera roll or take the photo, the app saves the photo locally and then goes to the next view controller and loads the image that was just saved and displayed on an image view.
This is the code that I used to get the image the user wanted and sent it to my send picture method.
UIImage *image = info[UIImagePickerControllerEditedImage];
[self saveImage:image];
[picker dismissViewControllerAnimated:NO completion:NULL];
[self performSegueWithIdentifier:#"switch" sender:nil];
Which is this:
-(void)saveImage: (UIImage*)image{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"test.png"];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
printf("image saved");
}
That code ran because it printed the image was saved. The app goes to the next view and this is how I set up my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
//[imageViewPreview setImage:self.secondImage];
//imageViewPreview.image = self.secondImage;
}
I commented out both ways that I've tried but neither of those works. This is how I was trying to get my secondImage to load:
-(UIImage* )secondImage{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"test.png"];
UIImage* image = [UIImage imageWithContentsOfFile:path];
printf("image loaded");
return image;
}
what is wrong here?
You should do this much simpler. In your second view controller add a public UIImage property. Then import the second controller into the first one. Then in prepareForSegue set it the image like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"switch"]) {
ClassOfDEstinationController *destinationController = segue.destinationViewController;
destinationController.image = image;
}
}

Writing/loading to/from file

Im trying to learn how to save/load images, and i just don't get why this wont work. Im writing a screenshot to the filesystem like this:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [directories objectAtIndex:0];
NSString *key = [documentsDirectory stringByAppendingPathComponent:#"screenshots.archive"];
[data writeToFile:key atomically:YES];
And in the "init" medthod in my UITableView subclass, i do this:
pics = [[NSMutableDictionary alloc]initWithContentsOfFile:[self dataFilePath]];
dataFilePath method:
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:#"screenshots.archive"];
}
To test if this works i have this delegate method:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return pics.count;
}
I test it by taking a screenshot, and then initializing my UITableview subclass, but it show no rows. What am i doing wrong?
There are a few key issues with the code that are causing it to not work. You're storing the image data directly to the file and trying to read it back as a dictionary. You'll want to wrap the image in an array first, and write the array to the file. Then you'll want to read the file into an array for the table to display. To sum up the changes:
Change
[data writeToFile:key atomically:YES];
to
NSMutableArray *storageArray = [NSMutableArray arrayWithContentsOfFile:key];
if(!storageArray)
storageArray = [NSMutableArray arrayWithObject:data];
else
[storageArray addObject:data];
[storageArray writeToFile:key atomically:YES];
and change
pics = [[NSMutableDictionary alloc]initWithContentsOfFile:[self dataFilePath]];
to
pics = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];

Saving an Image in App Directory instead of going to photo album. Objective C iOS

Hello I have an app that takes pictures but doesn't save. I know how to save the image to the Photo Album but I need the image to go to the app's directory and will not show up in the Photos gallery. I already have searched and I failed to look for a working one. I also need that image to be in my UIImageView when I open my app. Here are my codes:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *picker;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageView;
}
-(IBAction)TakePhoto;
-(IBAction)ChooseExisting;
- (IBAction)btnSave:(id)sender;
- (IBAction)btnLoad:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)TakePhoto{
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
// [picker release];
}
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"MyImage.png"] ];
image = [UIImage imageWithContentsOfFile:path];
return image;
}
-(IBAction)ChooseExisting{
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate=self;
[picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:NULL];
// [picker release];
}
- (IBAction)btnSave:(id)sender {
if (image != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"MyImage.png"]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
NSLog(#"saved");
}
}
- (IBAction)btnLoad:(id)sender {
[self loadImage];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[self loadImage];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The Save and loadImage methods don't work :(. No idea why but I don't have any errors.
Your image saving/loading code seems correct.
Check if your IBActions are properly connected in IB.
And change:
- (IBAction)btnLoad:(id)sender
{
[self loadImage];
}
to:
- (IBAction)btnLoad:(id)sender
{
[self loadImage];
[imageView setImage:image];
}
Your original code seems to load the image but it doesn't show it anywhere.
EDIT:
You can always check if the image is saved to documents directory.
With the device connected open the organizer window in XCode.
Select your device, then select applications. Choose the application
you're interested in and you should see the file-tree related to that
app. Is your image file listed in the Documents folder?
Check in documents directory for image. Your calling [self loadImage]; but loadImage return UIImage which your not using anywhere. You can attach data to picker like the following.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"MyImage.png"];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
[picker2 addAttachmentData:myData mimeType:#"image/png" fileName:#"MyImage"];

Resources