UIActivityViewController with custom UI - objective-c

UIActivityViewController
As shown in above the image , I want custom image and a text in marked places when I share data using UIActivityViewController from my app.
NSString *text = #"Custom Title";
NSURL *url = [NSURL URLWithString: [shareLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIImage *appIconImage = [UIImage imageNamed: #"IconImage.png"];
NSArray *array = [[NSArray alloc]initWithObjects:appIconImage,text ,url, nil];
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:array
applicationActivities:nil];
[controller setValue:#"Custom title" forKey:#"subject"];
controller.excludedActivityTypes = #[UIActivityTypePostToWeibo,
UIActivityTypePrint,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypeAirDrop];

Just pass the image urls to UIActivityViewController not the UIImage objects.
NSArray *array = [[NSArray alloc]initWithObjects:appIconImageURL,text ,url, nil];
Hope it helps!

Related

How can I share a gif image using UIActivityViewController with Objective-C?

I have an app that lets the user record a short video and generate a gif image. Now I want to give the user the possibility to share that gif image using UIActivityViewController, I've done this:
UIImage *img = ...
NSArray *objectsToShare = #[img];
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypePrint];
avc.excludedActivityTypes = excludeActivities;
[self presentViewController:avc animated:YES completion:nil];
The problem is that this only shares an static .jpg image (like the first frame of the original .gif image).
On the other hand, the user can save the gif image to the camera roll, and from there it is possible to share it as a .gif. So how can I do the same thing but from my own app?
Thanks in advance.
Ok, I managed to do this, and now I can share gif images by email, but I thing there is something left to do with Twitter.
UIImage *img = ...;
NSError *err;
NSData *imgData = [imageSerialization animatedGIFDataWithImage:img error:&err];
NSLog(#"%#", err);
NSArray *objectsToShare = #[imgData];
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypePrint, UIActivityTypeAssignToContact];
avc.excludedActivityTypes = excludeActivities;
[self presentViewController:avc animated:YES completion:nil];
Share GIF File For WhatsApp:
NSURL *imageUrl =[self.ImageArray objectAtIndex:currentPhotoIndex];
NSString *path=imageUrl.absoluteString;
NSArray *strings = [path componentsSeparatedByString:#"/"];
NSString *mygif=[strings objectAtIndex:strings.count-1];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataPath = [documentsPath stringByAppendingPathComponent:#"/MrHRamani"];
NSString *filePath = [dataPath stringByAppendingPathComponent:mygif];
NSURL *urll=[NSURL fileURLWithPath:filePath];
NSLog(#"imag %#",imageUrl);
self.documentationInteractionController.delegate = self;
self.documentationInteractionController.UTI = #"net.whatsapp.image";
self.documentationInteractionController = [self setupControllerWithURL:urll usingDelegate:self];
[self.documentationInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

UIActivityViewController how to convert string to print

I'm using UIActivityViewController to share some text from my app. I can send the text to messages, email, copy etc but I can't send it to print.
I see UIActivityTypePrint takes a datatype of NSData. How can I create UIActivityViewController to take my string as both a string and NSData so that the view has the print option available?
NSString *textToShare = self.note.note;
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo, nil];
Got it. It doesn't take NSData directly, it takes UISimpleTextPrintFormatter.
NSString *textToShare = self.note.note;
UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]
initWithText:textToShare];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,printData, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo, nil];
Add another item of NSData type in the array itemsToShare
NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare,data, nil];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo, nil];
OR
use dictionary to send item
NSString *textToShare = self.note.note;
NSData* data = [textToShare dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:textToShare forKey:#"shareString"];
[dictionary setObject:data forKey:#"shareData"];
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:dictionary, nil];
[dictionary release];
shareVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
shareVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo, nil];

iOS: Issue Saving Photo

I'm very new to Objective-C, and am having some beginner issues. I have an application that has an area that is supposed to behave somewhat like a photo gallery. The user chooses a picture from their camera roll, and the photos get displayed in UIImageViews. I'm trying to save the image that they select. I have 9 UIImageView's, and the issue is that when I select a different photo for each UIImageView, close and relaunch the app, the other 8 UIImageViews display the photo that is stored in the first image view. Here is the code that I'm working with:
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename9];
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(#"Image on didenterbackground: %#", imageView);
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView2.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView3.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView4.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView5.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView6.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView7.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView8.image)];
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(imageView9.image)];
[self.imageData writeToFile:[self dataFilePath] atomically:YES];
NSLog(#"The image is: %#", [[imageView image] description]);
NSLog(#"dataFilePath is: %#", [self dataFilePath]);
}
- (void)viewDidLoad
{
NSString *filePath = [self dataFilePath];
NSLog(#"FilePath: %#", filePath);
NSLog(#"Image: %#", imageView);
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *vdlData = [[NSData alloc] initWithContentsOfFile:filePath];
imageView.image = [[UIImage alloc] initWithData:vdlData];
imageView2.image = [[UIImage alloc] initWithData:vdlData];
imageView3.image = [[UIImage alloc] initWithData:vdlData];
imageView4.image = [[UIImage alloc] initWithData:vdlData];
imageView5.image = [[UIImage alloc] initWithData:vdlData];
imageView6.image = [[UIImage alloc] initWithData:vdlData];
imageView7.image = [[UIImage alloc] initWithData:vdlData];
imageView8.image = [[UIImage alloc] initWithData:vdlData];
imageView9.image = [[UIImage alloc] initWithData:vdlData];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
[super viewDidLoad];
}
I'm trying to figure out what I need to change to get the UIImageViews to display the correct pictures, rather than them all displaying the same picture. This is probably a simple fix, but any help would be greatly appreciated, thanks!
Okay, here's how I would do it:
Use NSUserDefaults to save your images as a mutable array:
ViewController.h
#property(retain) NSUserDefaults *user;
ViewController.m
#synthesize user;
- (void)viewWillAppear:(BOOL)animated
{
self.user = [NSUserDefaults standardUserDefaults];
Edit
NSMutableArray* array = [[self.user objectForKey:#"images"]mutableCopy];
while(array == nil)
{
[self.user setObject:[NSMutableArray arrayWithObject:#""] forKey:#"images"]
array = [[self.user objectForKey:#"images"]mutableCopy];
NSLog(#"%#",#"attempting to create an array to store the images in");
}
End Edit
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
NSLog(#"Image on didenterbackground: %#", imageView);
NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView5.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView6.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView7.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView8.image)];
[array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView9.image)];
[self.user setObject:array forKey:#"images"];
}
- (void)viewDidLoad
{
NSMutableArray* array = [[self.user objectForKey:#"images"]mutableCopy];
EDIT
if(array.count == 9)
{
imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]];
imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]];
imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]];
imageView4.image = [[UIImage alloc] initWithData:[array objectAtIndex:3]];
imageView5.image = [[UIImage alloc] initWithData:[array objectAtIndex:4]];
imageView6.image = [[UIImage alloc] initWithData:[array objectAtIndex:5]];
imageView7.image = [[UIImage alloc] initWithData:[array objectAtIndex:6]];
imageView8.image = [[UIImage alloc] initWithData:[array objectAtIndex:7]];
imageView9.image = [[UIImage alloc] initWithData:[array objectAtIndex:8]];
}
END EDIT
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.user = nil;
}
This way, you will not lose the images or data, they will be stored and easily accessed, and they will not disappear even if you update your app.
Cheers!
Before I start with the solution, I have to warn you that the way you're doing this isn't the right one. I suggest that you start learning iOS development from the ground up. Apple's own documentation is a pretty good start. http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html
Now, back to your question. What you do here is save only one image, not all 9 of them. You set self.imageData always with each image you process and you overwrite its previous value, making only the last image to be saved to file.
So, in order to make your code working, you would have to use an imageData object for each image view, then write that data object to file.
In your case, it's probably best to optimize the code by using loops, instead using multiple objects (like imageView, imageView2, ...).
Also, make sure that you take care of your memory. e.g. imageView.image is allocated but not released.
Well, I see two issues. First and foremost, in viewDidLoad, all your images are getting initWithData:vdlData... so they're all getting the same data. That's why they're all the same.
Also, when you're trying to save them, in ...didEnterBackground, you are overwriting the value of imageData over and over again... when you save it, it's just the last one you've assigned to imageData. You probably want to create an NSArray, and store them in there, pulling them out of the array in viewDidLoad.

UIImage error with dynamic string

I'm trying to load an image into the cell,
The following code is working correctly :
NSString *imageURL = #"http://localhost/images/image2.jpg";
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];
cell.image=rowImage;
but when i change imageURL to be dynamic , the image isn't displayed :
NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
You might be saying the problem is in :
[[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
But it's not , i've tried to log it and the output is as expected :
NSSLog(#"%#",[[stories objectAtIndex:indexPath.row] objectForKey:#"image"]);
//Outputs http://localhost/images/image2.jpg
What could be wrong ?
I am guessing there is space before or after the string. So do this,
NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
imageURL = [imageURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];
While this should mostly fix your issue, I suggest you trim the strings before adding to the dictionary during the XML parsing.
You should also ensure you release both imageData and rowImage. You might be doing it already but its not in the code above so I just wanted to indicate that it could be leaking.
try this one
NSString *imageURL =[NSString stringWithFormat:#"%#",[[stories objectAtIndex:indexPath.row] objectForKey:#"image"]];

Nothing displayed after splitting a list of image URLs

I have six image URLs in a string named aux. I split it with component separated by newlines. I want to pick images one by one from aux and display each in an ImageView. My code is:
aux = [_homeText.text stringByAppendingString:[NSString stringWithFormat:#" %# ",item.shortDescription]];
[_homeText setText:aux];
NSString *list =aux;
NSArray *listItems = [list componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[listItems count];
if ([listItems objectAtIndex:0]) {
NSURL *url = [NSURL URLWithString:aux];
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"hai" message:aux delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];*/
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[_homeImage setImage:image];
}
else if ([listItems objectAtIndex:1])
{
NSURL *url = [NSURL URLWithString:aux];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[_homeImage setImage:image];
}
There is no error but I get no image. Why? How do I fix this?
If both data and image not nil, you may try to correctly initialize _homeImage.
I don'w know why, but I think what you created _homeImage like this:
_homeImage = [[UIImageView alloc] init];
Try to do something like this:
CGSize imageSize = [image size];
[_homeImage setFrame:CGRectMake([_homeImage frame].origin.x,
[_homeImage frame].origin.y,
imageSize.width, imageSize.height)];
[_homeImage setImage:image];
[image release], image = nil;