【问题标题】:Instagram sharing of text overlayed image using UIActivityViewController failingInstagram 使用 UIActivityViewController 共享文本叠加图像失败
【发布时间】:2017-09-24 19:46:29
【问题描述】:

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

经过一番调查,如果发现无法与 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

【问题讨论】:

    标签: ios objective-c iphone instagram uiactivityviewcontroller


    【解决方案1】:

    我认为问题在于图像大小。当源图片(叠加文字)为 236 × 374 时,Instagram 分享失败。

    但是当我使用 442 × 620 png 图片作为基础图片时,我可以在叠加文字后分享图片。

    尽管有一个问题仍未得到解答。原版 236 × 374 图片(没有文字叠加)是如何通过 Instagram 成功分享的?

    【讨论】:

    • 我观察到,当我们以编程方式在图像上写入文本并保存时,有时它的分辨率会加倍,因此我们必须确保图像以正确的分辨率保存。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多