简体   繁体   中英

How to share image and text in objective c ios

I want to share image and text in all ActivityController but the following code is not work for me:

NSString *theFileName = [[fileNameOfImage lastPathComponent] stringByDeletingPathExtension];
//      [BDGSharing shareUsingActivityController:strDataForShare urlStr:@"google.com" image:_imageview1.image];


NSString *st = [NSString stringWithFormat:@"%@.jpg",theFileName];
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:st];
NSData *imageData2=UIImagePNGRepresentation(_imageview1.image);
[imageData2 writeToFile:saveImagePath atomically:YES];
NSURL *imageURL1=[NSURL fileURLWithPath:saveImagePath];
documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:imageURL1];
documentInteractionController.delegate = self;
documentInteractionController.name=strDataForShare;
 documentInteractionController.UTI = @"public.plain-text";
[documentInteractionController presentOptionsMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];

Please give me suggestion if any library or kit to share both on whatsapp.

Let's review your code. First-

NSString *st = [NSString stringWithFormat:@"%@.jpg",theFileName];
NSData *imageData2=UIImagePNGRepresentation(_imageview1.image);

You are saving a .jpg image with PNG representation, shouldn't you use jpeg representation instead?

UIImageJPEGRepresentation(_imageview.image, 1.0) 

Second-

[imageData2 writeToFile:saveImagePath atomically:YES];

Right here you are not sure wether the image was saved successfully or not. Please add a condition

if([imageData2 writeToFile:saveImagePath atomically:YES]){

// here you are sure that the image was saved and you can use it.

}

Third-

documentInteractionController.UTI = @"public.plain-text";

You are sharing a jpg image, why is your UTI plain-text? Shouldn't it be public.image ?

Just try to solve those issue and your code should work successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM