崩溃文攻 “在打开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]中。
答
它看起来像你没有保留文件控制器。我用的是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;
}
让我知道,如果这有助于。
答
我也面临同样的问题,声明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;
}
希望这有助于! :)
什么是崩溃?未捕获的异常?访问不好?越界访问? –
查看我更新的回复,您应该能够复制并粘贴我创建的方法并将其用于您的应用。希望能帮助到你。 – dana0550
访问错误。我发现这个问题,这只是分配器对象的释放过早。 – madewulf