Post image to Instagram with Swift (code needed to be translated)

I found this code in another custom question, but its objective-c and I need it in Swift. I tried to translate it, but when Apple's sharing dialog comes up and I touch Instagram, the app crashes.

The code is this: from How to share an image on Instagram on iOS?

UIImage *screenShot = finalImage;

NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.igo"];

[UIImagePNGRepresentation(screenShot) writeToFile:savePath atomically:YES];

CGRect rect = CGRectMake(0 ,0 , 0, 0);
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.igo"];
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:@"file://%@", jpgPath]];

self.dic = [UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
self.dic.UTI = @"com.instagram.photo";
self.dic.annotation = [NSDictionary dictionaryWithObject:@"Enter your caption here" forKey:@"InstagramCaption"];

[self.dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"];

if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {

    [self.dic presentOpenInMenuFromRect: rect    inView: self.view animated: YES ];

} else {

    NSLog(@"No Instagram Found");
}

      

Of course, in the example, the user is using the finalImage to declare the screenShot UIImage, so I just did this to try: (I have posteo-01 resource in the project)

func postImage() {
    var imagePost = UIImage(named: "posteo-01")
    var savePath = documentsDirectory().stringByAppendingPathComponent("Test.igo")
    UIImageJPEGRepresentation(imagePost, 100).writeToFile(savePath, atomically: true)
    var rect = CGRectMake(0, 0, 0, 0)
    var igImageHookFile = NSURL(string: "file://\(savePath)")
    var dic = UIDocumentInteractionController(URL: igImageHookFile!)
    dic.UTI = "com.instagram.photo"
    dic.annotation = ["InstagramCaption": "hola"]
    dic.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
    var instagramURL = NSURL(string: "instagram://media?id=MEDIA_ID")

    if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
        dic.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)
    } else {
        println("no instagram found")
    }

}
func documentsDirectory() -> String {
    let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
    return documentsFolderPath
}

      

Thank!

Already solve this:

var docController = UIDocumentInteractionController()

func postImage2() {
    var instagramURL = NSURL(string: "instagram://app")

    if(UIApplication.sharedApplication().canOpenURL(instagramURL!)) {
        var imagePost = UIImage(named: "posteo-01")
        var fullPath = documentsDirectory().stringByAppendingPathComponent("insta.igo")
        var imageData = UIImagePNGRepresentation(imagePost).writeToFile(fullPath, atomically: true)
        var rect = CGRectMake(0, 0, 0, 0)
        self.docController.UTI = "com.instagram.exclusivegram"
        var igImageHookFile = NSURL(string: "file://\(fullPath)")
        self.docController = UIDocumentInteractionController(URL: igImageHookFile!)
        self.docController.annotation = ["InstagramCaption":"Text"]
        self.docController.presentOpenInMenuFromRect(rect, inView: self.view, animated: true)

    } else {
        println("no instagram found")
    }

}

func documentsDirectory() -> String {
    let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
    return documentsFolderPath
}

      

+3


source to share





All Articles