Uploading photos to Instagram via your own iOS app

后端 未结 8 1449
执笔经年
执笔经年 2020-12-23 09:57

Instagram recently made a change to their API policy which allowed developers to post pictures to the Instagram platform via their own app. Several other techniques we\'re p

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 10:39

    if you want to just send the image without clipping you should set image size of 640 x 640 or the application crash

    This my swift code

     var instagramURL = NSURL(string: "instagram://app")!
            if UIApplication.sharedApplication().canOpenURL(instagramURL) {
                var documentDirectory = NSHomeDirectory().stringByAppendingPathComponent("Documents")
                var saveImagePath = documentDirectory.stringByAppendingPathComponent("Image.igo")
                var imageData = UIImagePNGRepresentation(self.bestScoreShareView.takeSnapshot(W: 640, H: 640))
                imageData.writeToFile(saveImagePath, atomically: true)
                var imageURL = NSURL.fileURLWithPath(saveImagePath)!
               docController  = UIDocumentInteractionController()
                docController.delegate = self
                docController.UTI = "com.instagram.exclusivegram"
                docController.URL = imageURL
                docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
    
            } else {
                println("instagram not found")
            }
    

    extension for take shapshot from uiview

    extension UIView {
        func takeSnapshot(#W: CGFloat, H: CGFloat) -> UIImage {
            var cropRect = CGRectMake(0, 0, 600, 600)
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
            drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            image.imageWithNewSize(CGSizeMake(W, H))
            return image
        }
    }
    

    extension for set image size

    extension UIImage {
         func imageWithNewSize(newSize:CGSize) ->UIImage {
            UIGraphicsBeginImageContext(newSize)
            self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    
            let newImage = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
            return newImage
        }
    }
    

提交回复
热议问题