首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UIActivityViewController的Instagram文本叠加图像共享失败

使用UIActivityViewController的Instagram文本叠加图像共享失败
EN

Stack Overflow用户
提问于 2017-09-25 03:46:30
回答 1查看 421关注 0票数 0

我正在使用UIActivityViewController和子类化UIActivityItemSource通过安装在我的iPhone上的应用程序共享文本和图像。

经过一番调查,发现无法与Instagram应用程序分享“文字”和“图片”。

因此,我们决定将文本(Instagram标题)覆盖到图片本身(静态图片,在我的例子中是Lion.png,包含在资源文件夹中)。但我发现,如果我使用Instagram应用程序(使用UIActivityViewController显示)来共享“文本叠加图像”,尽管Instagram应用程序启动时会显示图像,但当我输入标题并点击共享按钮时,虽然似乎共享成功,但图像并未被共享。

通过电子邮件客户端共享修改后的png成功。不确定为什么Instagram会失败。

如果我决定通过Instagram共享没有“文本覆盖”的原始图像,那么在Instagram上共享是成功的。

注意:下面的代码,我已经从我的项目中提取并放入了一个示例项目中。

代码语言:javascript
复制
#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。

代码语言:javascript
复制
//
//  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
EN

回答 1

Stack Overflow用户

发布于 2017-09-27 14:42:46

我认为这个问题是因为图像大小。当源图像(叠加文本)为236 × 374时,Instagram共享失败。

但当我使用442 × 620png图像作为基础图像时,我可以在叠加文本后共享图像。

尽管有一个问题仍然没有得到回答。香草236 × 374图片(没有文字叠加)是如何在Instagram上成功分享的?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46394174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档