问题
I have got the following class:
.h file :
#import <Foundation/Foundation.h>
@interface CHTInstagramSharer : NSObject<UIDocumentInteractionControllerDelegate>
@property (nonatomic, retain) UIDocumentInteractionController *dic;
-(void) sharePic:(UIImage *)image;
@end
.m file
#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
It presents the controller with the option "Open in Instagram", but when I tap on that, the app crashes.
Any idea why?
Complementary information, when I look at the url in the debugger, I get: file://localhost/var/mobile/Applications/53435781-3BAB-4B02-A80C-FC088F696AE8/Library/Caches/cover.igo
The crash seems to happen in [_UIOpenWithAppActivity performActivity] when I look in the stack trace.
回答1:
It looks like you are not retaining the document controller. I use the Instagram hook like this:
EDIT: Modified for 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;
}
Let me know if this helps.
回答2:
Me too face the same issue, Declaring the UIDocumentInteractionController *docController
commonly on @interface
worked for me!
@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;
}
Hope this helps! :)
来源:https://stackoverflow.com/questions/16571212/crash-wen-tapping-open-in-instagram-while-using-uidocumentinteractioncontrolle