내 iPhone에 설치된 앱을 통해 텍스트 및 이미지를 공유하기 위해 UIActivityViewController를 사용하고 UIActivityItemSource를 서브 클래 싱하는 중입니다.Instagram에서 UIActivityViewController를 사용하여 이미지 오버레이 이미지를 공유하지 못했습니다.
Instagram 앱에서 "텍스트"와 "이미지"를 공유 할 수없는 경우 조사가 끝난 후 발견되었습니다.
이미지 자체 (정적 이미지, 내 경우 Lion.png, 리소스 폴더에 포함) 위에 텍스트 (Instagram 캡션)를 오버레이하기로 결정했습니다. 하지만 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