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
post ios image swift instagram


source to share


No one has answered this question yet

See similar questions:

81
How to share an image to Instagram on iOS?

or similar:

928
How do I call Objective-C code from Swift?
376
Upload / download image from url in Swift
304
Configuring WPF Image Source in Code
166
Swift HTTP request with POST method
81
How to share an image to Instagram on iOS?
13
How to share images on instagram? Swift
3
Post to Instagram screen on iOS Swift
1
Black screen For video game url comes from Server Side
0
Add comment to image on instagram
0
sending image as POST to SWIFT



All Articles
Loading...
X
Show
Funny
Dev
Pics