![](/img/trans.png)
[英]Sharing image and text to Facebook Messenger with UIActivityViewController failing
[英]Instagram sharing of text overlayed image using UIActivityViewController failing
我正在使用UIActivityViewController並將UIActivityItemSource子類化以通過iPhone上安裝的應用程序共享文本和圖像。
經過一番調查后,If發現無法與Instagram應用程序共享“文本”和“圖像”。
因此,我們決定將文本(Instagram標題)覆蓋在圖像本身上(靜態圖像,在我的情況下是Lion.png,包含在資源文件夾中)。 但是我發現,如果我要使用Instagram應用程序(使用UIActivityViewController顯示)共享“文本疊加的圖像”,盡管Instagram應用程序隨圖像一起啟動,但是當我輸入標題並按下“共享”按鈕時,盡管看起來共享成功,但是圖像未共享。
通過電子郵件客戶端成功共享修改后的png。 不知道為什么Instagram失敗了。
如果我決定通過Instagram分享不帶“文字疊加”的原始圖像,則分享在Instagram上是成功的。
注意:以下代碼是我從項目中提取的,並放入了一個示例項目。
#import "ViewController.h"
#import "EmailItemProvider.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[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.
}
-(UIImage*) drawText:(NSString*) text
inImage:(UIImage*) image
atPoint:(CGPoint) point
{
UIFont *font = [UIFont boldSystemFontOfSize:14];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
// [[UIColor whiteColor] set];
// [text drawInRect:CGRectIntegral(rect) withFont:font];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor whiteColor],NSParagraphStyleAttributeName: paragraphStyle };
// draw text
[text drawInRect:rect withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (NSString*)saveImageFile:(UIImage *)uiimage
{
NSData *data = UIImagePNGRepresentation(uiimage);
NSString *filePath = [NSString stringWithFormat:@"%@/sample.png" ,[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
[data writeToFile:filePath atomically:YES];
return filePath;
}
#define SEND_TO_MESSAGE @"Share via Message"
#define SEND_TO_MAIL @"Share via Mail"
- (IBAction)ShareOptions:(id)sender {
UIImage *annotatedFile = [self drawText: @"Referral msg with code" inImage:[UIImage imageNamed:@"Lion"] atPoint: CGPointMake(0, 0)];
NSString *imageFilePath = [self saveImageFile:annotatedFile];
NSMutableDictionary *shareOptionDic=[[NSMutableDictionary alloc] init];
[shareOptionDic setObject:SEND_TO_MESSAGE forKey:@"1"];
[shareOptionDic setObject:SEND_TO_MAIL forKey:@"2"];
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setString:@"Referral message copied to the clipboard."];
EmailItemProvider *emailItem = [EmailItemProvider new];
emailItem.subject = @"sample subject";//Dummy. overridden in the delegate methods of EmailItemProvider.
emailItem.body = @"sample body";//Dummy. overridden in the delegate methods of EmailItemProvider.
//Image with the text overlay. When this image is used, the Instagram share fails.
emailItem.imagePath = imageFilePath;
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[emailItem]
applicationActivities:nil];
activityViewController.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint,UIActivityTypeAirDrop];
[self presentViewController:activityViewController animated:TRUE completion:nil];
return;
}
@end
EmailItemProvider類是UIActivityItemSource的子類,下面提供了.h和.m。
//
// EmailItemProvider.h
//
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface EmailItemProvider : NSObject <UIActivityItemSource>
@property (nonatomic, strong) NSString *subject;
@property (nonatomic, strong) NSString *body;
@property (nonatomic, strong) UIImage *image;//dummy
@property (nonatomic, strong) NSString *imagePath;//image path with text overlay
@end
//
// EmailItemProvider.m
//
//
#import "EmailItemProvider.h"
@implementation EmailItemProvider
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
//This code works.
//return [UIImage imageNamed:@"Lion"];
//Returning an text overlayed image for Instagram share doesnot work.
return [UIImage imageWithContentsOfFile:self.imagePath];
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
NSLog(@"one %@", activityType);
//This code which return an image overlayed with text, instagram share fails.
return @{@"text": @"Referral information goes here.", @"image": [UIImage imageWithContentsOfFile:self.imagePath]};
//I am able to share Instagram share when I comment the above code and uncomment the below code.
//return @{@"text": @"Referral information goes here.", @"image": [UIImage imageNamed:@"Lion"]};
}
- (nullable UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(nullable UIActivityType)activityType suggestedSize:(CGSize)size; // if activity supports preview image. iOS 7.0
{
NSLog(@"two activity type : %@\n", activityType);
return [UIImage imageNamed:@"Lion"];
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
NSLog(@"three %@", activityType);
return @"subject text";
}
@end
我認為該問題是由於圖像尺寸所致。 當源圖像(帶有文本的附加圖像)為236×374時,Instagram共享失敗。
但是,當我使用442×620 png圖像作為基本圖像時,可以在疊加文本后共享圖像。
盡管一個問題仍然沒有答案。 如何通過Instagram成功共享原始236×374圖像(無文本疊加)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.