崩溃文攻 “在打开Instagram的”,而使用UIDocumentInteractionController

问题描述:

我有下面的类:崩溃文攻 “在打开Instagram的”,而使用UIDocumentInteractionController

.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 

它提出控制器选择“在Instagram中打开”,但是当我点击该选项时,该应用程序崩溃。

任何想法为什么?

补充信息,当我看到在调试器的网址,我得到: 文件://localhost/var/mobile/Applications/53435781-3BAB-4B02-A80C-FC088F696AE8/Library/Caches/cover.igo

当我查看堆栈跟踪时,崩溃似乎发生在[_UIOpenWithAppActivity performActivity]中。

+0

什么是崩溃?未捕获的异常?访问不好?越界访问? –

+0

查看我更新的回复,您应该能够复制并粘贴我创建的方法并将其用于您的应用。希望能帮助到你。 – dana0550

+1

访问错误。我发现这个问题,这只是分配器对象的释放过早。 – madewulf

它看起来像你没有保留文件控制器。我用的是Instagram的钩子是这样的:

编辑:修改为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; 
} 

让我知道,如果这有助于。

+0

谢谢,但是如果你进一步阅读的话,还有:另外,如果你只想在应用程序列表中显示Instagram(而不是Instagram加上任何其他符合公共/ jpeg标准的应用程序),你可以指定扩展类igo,其类型为com.instagram.exclusivegram。 – madewulf

+0

我明白了,你是对的。在使用API​​钩子之后,必须添加它。阅读我编辑的回复,看起来你没有保留DocumentController。 – dana0550

+0

那是因为我正在使用ARC。 – madewulf

我也面临同样的问题,声明UIDocumentInteractionController *docController通常在@interface为我工作!

@interface ViewController() 
{ 
UIDocumentInteractionController * docController; 
} 
@end 

- (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]; 

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; 
} 

希望这有助于! :)