mglagola / MGInstagram

A Simple and Easy to use iOS Instagram Utility for posting images to Instagram

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

App crash after selecting instagram in UIView

mingchuno opened this issue · comments

I have tried to share some image to instagram in my own app. I used this API, after pressing the instagram icon in the Open In Menu, my app crash with no reason. Any one knows why?

I found the problem. When positing the image, the documentInteractionController will Auto release , so we must retain them but at the same time need to release them when finish using it. So I have modified the code a bit:

+ (void) postImage:(UIImage*)image withCaption:(NSString*)caption inView:(UIView*)view {
    [self postImage:image withCaption:caption inView:view delegate:[self sharedInstance]];
}

- (void) postImage:(UIImage*)image withCaption:(NSString*)caption inView:(UIView*)view delegate:(id<UIDocumentInteractionControllerDelegate>)delegate
{
    if (!image)
        [NSException raise:NSInternalInconsistencyException format:@"Image cannot be nil!"];

    [UIImageJPEGRepresentation(image, 1.0) writeToFile:[self photoFilePath] atomically:YES];

    NSURL *fileURL = [NSURL fileURLWithPath:[self photoFilePath]];
    documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    documentInteractionController.UTI = @"com.instagram.exclusivegram";
    documentInteractionController.delegate = delegate;
    if (caption)
        documentInteractionController.annotation = [NSDictionary dictionaryWithObject:caption forKey:@"InstagramCaption"];
    [documentInteractionController retain];
    [documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:view animated:YES];

}

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
    [controller release];
}

@mingchuno This project uses ARC!