我正在使用UIActivityViewController和UIActivityItemSource的子類來通過安裝在我的iPhone上的應用程序共享文本和圖像。使用UIActivityViewController失敗的Instagram共享文本覆蓋圖像
經過一番調查,如果發現不可能與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