【问题标题】:Crash wen tapping "Open in Instagram" while using UIDocumentInteractionController使用 UIDocumentInteractionController 时点击“在 Instagram 中打开”时崩溃
【发布时间】:2013-05-15 17:16:51
【问题描述】:

我有以下课程:

.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 中打开”选项,但是当我点击该选项时,应用程序崩溃了。

知道为什么吗?

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

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

【问题讨论】:

  • 什么是崩溃?未捕获的异常?访问不方便?越界访问?
  • 查看我的更新回复,您应该可以复制并粘贴我创建的方法并在您的应用中使用它。希望对您有所帮助。
  • 访问不正确。我发现了问题,只是共享对象的释放太早了。
  • 我似乎无法将注释转移到 Instagram - 标题只是空的。有人有这方面的经验吗?

标签: ios objective-c instagram


【解决方案1】:

您似乎没有保留文档控制器。我像这样使用 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;
}

如果这有帮助,请告诉我。

【讨论】:

  • 谢谢,但如果您进一步阅读,有:可以指定扩展类igo,类型为com.instagram.exclusivegram。
  • 我明白了,你是对的。这一定是在我使用 API 挂钩之后添加的。阅读我编辑的回复,您似乎没有保留 DocumentController。
  • 那是因为我用的是ARC。
  • 看看我更新的回复,我修改了它以与 ARC 一起使用。此外,您仍然需要保留文档控制器。这就是为什么它会为你崩溃。一件事,您不应该对 CGRect 值进行硬编码。使用我的示例,因为这样您就不必担心设备屏幕尺寸会发生变化。另一件事是,您是否需要 DocumentController 的属性?为什么不在需要时实例化对象?
  • 使用 ARC 时,您可以使 UIDocumentInteractionController 成为一个强属性并且它可以工作。
【解决方案2】:

我也面临同样的问题,通常在 @interface 上声明 UIDocumentInteractionController *docController 对我有用!

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

希望这会有所帮助! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多