我有以下类(class):
.h 文件:
#import <Foundation/Foundation.h>
@interface CHTInstagramSharer : NSObject<UIDocumentInteractionControllerDelegate>
@property (nonatomic, retain) UIDocumentInteractionController *dic;
-(void) sharePic:(UIImage *)image;
@end
.m文件
#import "CHTInstagramSharer.h"
#import <UIKit/UIKit.h>
#import "BaseController.h"
@implementation CHTInstagramSharer
-(void) sharePic:(UIImage *)image{
NSString * jpgPath = [Utils saveImage:image toFile:@"cover.igo"];
NSURL *url = [NSURL fileURLWithPath:jpgPath];
self.dic = [UIDocumentInteractionController interactionControllerWithURL: url];
self.dic.UTI = @"com.instagram.exclusivegram";
self.dic.delegate = self;
self.dic.annotation = [NSDictionary dictionaryWithObject:@"Caption Test" forKey:@"InstagramCaption"];
[self.dic presentOpenInMenuFromRect:CGRectMake(0, 0, 320, 44) inView:[BaseController sharedInstance].view animated: YES ];
}
@end
它向 Controller 显示“在 Instagram 中打开”选项,但当我点击它时,应用程序崩溃了。
知道为什么吗?
补充信息,当我在调试器中查看 url 时,我得到: file://localhost/var/mobile/Applications/53435781-3BAB-4B02-A80C-FC088F696AE8/Library/Caches/cover.igo
当我查看堆栈跟踪时,崩溃似乎发生在 [_UIOpenWithAppActivity performActivity] 中。
最佳答案
看起来您没有保留文档 Controller 。我这样使用 Instagram Hook :
编辑:为 ARC 修改
- (void)sharePic:(UIImage *)image {
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image.ig"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
[imageData writeToFile:savedImagePath atomically:YES];
NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:imageUrl];
[docController retain];
docController.UTI = @"com.instagram.exclusivegram";
docController.delegate = self;
docController.annotation = [NSDictionary dictionaryWithObject:@"Caption Test" forKey:@"InstagramCaption"];
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
docController = nil;
}
如果这有帮助,请告诉我。
关于ios - 使用 UIDocumentInteractionController 时点击 "Open in Instagram"时发生崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571212/