Instagram使用UIActivityViewController共享文本覆盖图像失败

时间:2017-09-24 19:46:30

标签: ios objective-c iphone instagram uiactivityviewcontroller

我正在使用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

1 个答案:

答案 0 :(得分:0)

我认为这个问题是因为图像尺寸。当源图像(超强加文字)为236×374时,Instagram的份额失败了。

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

虽然有一个问题仍然没有答案。如何通过Instagram成功分享香草236×374图像(没有文字叠加)?