I'm trying to share a photo to Instagram. So far my method is like this:
- (void)shareToInsta:(UIImage *)finalImage
{
NSString *savePath = [NSHomeDirectory() stringByAppendingString:#"Documents/instagramTmp.igo"];
[UIImageJPEGRepresentation(finalImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"com.instagram.exclusivegram";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
The Instagram option is presented in UIDocumentInteractionController, but when I choose it, nothing happens. Anyone knows what I'm doing wrong?
PS: I'm already using a method just like this, but for WhatsApp, and it works just fine.
Save the file is wrong
- (void) saveimage:(UIImage*)image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"save.igo"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
and
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/save.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
Related
I'm getting base64-encoded file content from server. Then I'm writing data into file. I checked that file is not empty after that, it means data was successfully written to file. But when I'm trying to present this file, all I get is an empty view. So what am I doin' wrong?
enter image description here
[f getFile:^(NSString * _Nonnull string) {
NSString *docsDir;
NSArray *dirPaths;
NSData *content = [string dataUsingEncoding:NSUTF8StringEncoding];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:#"hill.jpg"]];
[content writeToFile:databasePath atomically:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:databasePath]){
NSURL *url = [NSURL fileURLWithPath:databasePath];
UIDocumentInteractionController *documentInteractionController =[UIDocumentInteractionController interactionControllerWithURL:url];
documentInteractionController.delegate = self;
[documentInteractionController presentPreviewAnimated:YES];
}
} Error:^(NSString * _Nonnull errorMessage) {
}];
Edited your code snippet, Try it
[f getFile:^(NSString * _Nonnull string) {
NSString *docsDir;
NSArray *dirPaths;
NSData *content = [[NSData alloc] initWithBase64EncodedString: string options:0];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:#"hill.jpg"]];
[content writeToFile:databasePath atomically:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:databasePath]){
NSURL *url = [NSURL fileURLWithPath:databasePath];
UIDocumentInteractionController *documentInteractionController =[UIDocumentInteractionController interactionControllerWithURL:url];
documentInteractionController.delegate = self;
[documentInteractionController presentPreviewAnimated:YES];
}
} Error:^(NSString * _Nonnull errorMessage) {
}];
From your code, I see you save a string into a an image file (hill.jpg). The UIDocumentInteractionController read file base on their extension, so they can't read this file. I think if you change your file name to hill.txt, your code could work. Hope this helps.
As you say, the content gets from the server are base64-encode, so you need to decode it.
NSData *imageContent = [[NSData alloc] initWithBase64EncodedString:string options:kNilOptions];
if (imageContent) {
// save to file
}
I am using following code for getting single image. But, how to do it for multiple images?
This is my code :
-(void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(loadImageIntoMemory) withObject:nil];
}
-(void)loadImageIntoMemory {
NSLog(#"Beginning download");
NSString *temp_Image_String = [[NSString alloc] initWithFormat:#"http://www.thewavestore.com/appproductimage/"];
NSURL *url_For_Ad_Image = [[NSURL alloc] initWithString:temp_Image_String];
NSData *data_For_Ad_Image = [[NSData alloc] initWithContentsOfURL:url_For_Ad_Image];
UIImage *temp_Ad_Image = [[UIImage alloc] initWithData:data_For_Ad_Image];
[self saveImage:temp_Ad_Image];
UIImageView *imageViewForAdImages = [[UIImageView alloc] init];
imageViewForAdImages.frame = CGRectMake(0, 0, 320, 480);
imageView.image = [self loadImage];
[self.view addSubview:imageView];
}
-(void)saveImage:(UIImage *)image {
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: #"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
-(UIImage *)loadImage {
NSLog(#"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Please help to over come from this issues,if any good idea please suggest me.
First save your images with unique names (i am using current time) and save this names to an array
NSData *imageData = UIImagePNGRepresentation(chosenImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
// save image with currentdate and time in the name
NSString * imageName = [NSString stringWithFormat:#"name_%#.png",[dateFormatter stringFromDate:[NSDate date]]];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
//add all the images names to the imagepath array
[imagePathArray addObject:imageName];
//write image into file
[imageData writeToFile:fullPathToFile atomically:YES];
then retrive images from image path array
for (NSString*path in imagePathArray ) {
NSArray *filepart = [path componentsSeparatedByString:#"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/%#",
documentsDirectory,path];
//NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:fileName];
}
1. Store the server images in to array.
NsMutableArray *arrImages=[[nsmutableArray alloc]init];
2.Get the count of the array to run this in for loop.
for(int i=0;i<[arrImages count];i++)
{
NSLog(#"Saving");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent: arrImages[i]];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
To convert it to JPEG
NSData=UIImageJPEGRepresentation(UIImage,1.0);
I'm trying to create an application where it opens instagram after editing the picture in my app. I wanted it to include a hashtag on the description when it goes to instagram but I seem to get lost on how to implement that. The iphone hooks states to use the tag line but I don't know where to place that. below is my code
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
docInteraction.delegate = self;
Saves the edited Image to directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"instaFilterx.jpg"];
UIImage *image = combinedImage;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
//Loads the edited Image
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:#"instaFilterx.jpg"];
UIImage *tempImage = [UIImage imageWithContentsOfFile:getImagePath];
Hooks the edited Image with Instagram
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/instaFilterx.igo"];
[UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:jpgPath atomically:YES];
Prepares the DocumentInteraction with the .igo image for Instagram
NSURL *instagramImageURL = [[NSURL alloc] initFileURLWithPath:jpgPath];
docInteraction = [UIDocumentInteractionController
interactionControllerWithURL:instagramImageURL];
[docInteraction setUTI:#"com.instagram.exclusivegram"];eciate any help. Thank you.
To add caption to your Instagram Post. Do the following:
docInteraction.annotation=[NSDictionary dictionaryWithObjectsAndKeys:#"Your #Text", #"InstagramCaption", nil];
Hope this helps.
I have files recorded by the user in the documents directory... displayed in a uitableview...
I placed this code in my didSelectRowAtIndexPath... Why doesn't this work?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer addAttachmentData:audioData mimeType:#"audio/caf" fileName:fileName];
You are creating the URL incorrectly. You need to use fileURLWithPath, not URLWithString.
Also, this:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",[directoryContents objectAtIndex:indexPath.row]]];
Should be:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[directoryContents objectAtIndex:indexPath.row]];
There is no need for the string format.
Last thing. Th file name passed to the addAttachment method should be just a file name, not a full pathname.
Is there any way to save a screenshot to the application's sandbox or some where that can be easily found to add to an email attachment? currently im saving it in the camera roll but can't seem to be able to get it to attach to my email
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *link = [NSString stringWithFormat: #"%#%#%#", documentDirectory,filename,png];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:link];
NSLog(link);
UIImageWriteToSavedPhotosAlbum(viewImage, drawquestion, nil, nil);
Here are 2 bare bones examples of how to attach an image to an email.
// with UIImage * image;
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImagePNGRepresentation(image);
[mfcvc addAttachmentData:imageData mimeType:#"image/png" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
// with UIImage * image; and float compression_quality; between 0.0 and 1.0
MFMailComposeViewController * mfcvc = [[[MFMailComposeViewController alloc] init] autorelease];
NSData * imageData = UIImageJPEGRepresentation(image, compression_quality);
[mfcvc addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"demo"];
[self.viewController presentModalViewController:mfcvc animated:YES];
To save the image in the documents directory in the app's sandbox
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *png = #".png";
NSString *filename = [drawquestion stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *imagePath = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%#",filename, png]];
[imageData writeToFile:imagePath atomically:YES];
PS
I cleaned up the construction of the path, it seems your code has a path like
/var/mobile/Applications/APP_ID//var/mobile/Applications/APP_ID/Documents/filename.png
You should check out writeToFile:atomically: you get your image data, and write it to a file, this will save to the application's sandbox.
Check out this for some example code: http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(#"%#",docDir);
NSLog(#"saving png");
NSString *pngFilePath = [NSString stringWithFormat:#"%#/test.png",docDir];
// You should swap data for your image data
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
You can then use the addAttachmentData:mimeType:fileName: method on MFMailComposeViewController class to attach an attachment (actually you don't even need to save the image to disk):
MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];
NSData *data; // this is the data from earlier
[mailVC addAttachmentData:data mimeType:#"image/png" fileName:#"myfilename"];
[self presentModalViewController:mailVC animated:YES];
Be sure to have a correct MIME type set here. The file name you can set to what you want, it is the name of the file in the attachment as seen by the recipient.