0

I am 100% sure this has been answered but the answers I am finding I cannot seem to make fit into my solution.

Starting from the beginning I have my ShareViewController where I have two imageViews put one on one, and in order to make it one image I am taking a screenshot of that imageView size. The code for taking screenshot I have taken here iOS: what's the fastest, most performant way to make a screenshot programmatically?

Here is my class:

import UIKit
import Photos

class ShareViewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var defaultImage: UIImage? = nil
    var documentController: UIDocumentInteractionController!

    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var overlayImageView: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        guard let image = defaultImage else {
            return
        }

        self.imageView.image = image
        self.imageView.contentMode = .ScaleAspectFill

        let subimage1 = UIImage(named: "overlay")
        self.overlayImageView.image = subimage1
        self.imageView.clipsToBounds = true

    }

    @IBAction func shareButton(sender: UIButton) {
        self.screenshot()
        self.shareToInstagram()
        _ = self.imageView.image!
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
        if let lastAsset = fetchResult.firstObject as? PHAsset {
            let localIdentifier = lastAsset.localIdentifier
            let u = "instagram://library?LocalIdentifier=" + localIdentifier
            _ = NSURL(string: u)!

        }
    }

    // MARK: - Make Screenshot of ImageView

    class func screenshot() -> UIImage {
        var imageSize = CGSizeZero

        let orientation = UIApplication.sharedApplication().statusBarOrientation
        if UIInterfaceOrientationIsPortrait(orientation) {
            imageSize = UIScreen.mainScreen().bounds.size
        } else {
            imageSize = CGSize(width: UIScreen.mainScreen().bounds.size.height, height: UIScreen.mainScreen().bounds.size.width)
        }

        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
        let context = UIGraphicsGetCurrentContext()
        for window in UIApplication.sharedApplication().windows {
            CGContextSaveGState(context)
            CGContextTranslateCTM(context, window.center.x, window.center.y)
            CGContextConcatCTM(context, window.transform)
            CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y)
            if orientation == .LandscapeLeft {
                CGContextRotateCTM(context, CGFloat(M_PI_2))
                CGContextTranslateCTM(context, 0, -imageSize.width)
            } else if orientation == .LandscapeRight {
                CGContextRotateCTM(context, -CGFloat(M_PI_2))
                CGContextTranslateCTM(context, -imageSize.height, 0)
            } else if orientation == .PortraitUpsideDown {
                CGContextRotateCTM(context, CGFloat(M_PI))
                CGContextTranslateCTM(context, -imageSize.width, -imageSize.height)
            }
            if window.respondsToSelector(#selector(UIView.drawViewHierarchyInRect(_:afterScreenUpdates:))) {
                window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
            } else if let context = context {
                window.layer.renderInContext(context)
            }
            CGContextRestoreGState(context)
        }

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

    // MARK: - Share photo to Instagram

    func displayAlert(title: String, message: String) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(alertController, animated: true, completion: nil)
        return
    }

    func displayShareSheet(shareContent:String) {
        let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
        presentViewController(activityViewController, animated: true, completion: {})
    }


    func shareToInstagram() {

        let instagramURL = NSURL(string: "instagram://app")

        if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) {

            let imageData = UIImageJPEGRepresentation(self.imageView.image!, 100)

            let captionString = "caption"

            let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")
            if imageData?.writeToFile(writePath, atomically: true) == false {

                return

            } else {
                let fileURL = NSURL(fileURLWithPath: writePath)

                self.documentController = UIDocumentInteractionController(URL: fileURL)

                self.documentController.delegate = self

                self.documentController.UTI = "com.instagram.exlusivegram"

                self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption")
                self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true)

            }

        } else {
            print(" Instagram isn't installed ")
        }
    }
}

Now when I try to call screenshot() method it says that

Static member 'screenshot' cannot be used on instance of type 'ShareViewController'

How can I fix this and successfully take screenshot of my imageViews?

Community
  • 1
  • 1
Anuar Maratkhan
  • 325
  • 5
  • 21
  • remove `class` definition of a func – Özgür Ersil Aug 09 '16 at 07:43
  • Or call it like ShareViewController.screenshot() since you declared it as class method – Yury Aug 09 '16 at 07:44
  • it didnt help to solve my problem. i am not taking screenshot – Anuar Maratkhan Aug 09 '16 at 07:46
  • I don't see if you trying to use result of screenshot() method. – Yury Aug 09 '16 at 07:49
  • @ShadowOf how can I use it? and how to take screenshot of size equal to the size of imageview? I am to stupid to do it myself – Anuar Maratkhan Aug 09 '16 at 08:01
  • let screenshot = ShareViewController.screenshot(); I don't know answer to last question, was never needed. Why you can't use image property of your UIImageView? – Yury Aug 09 '16 at 08:04
  • @ShadowOf because as you see I am making two imageviews which are put one on one. For instance, I have one image view and a subview which is put on an imageview, and when I want to share it I am sharing only imageview.image, without subview – Anuar Maratkhan Aug 09 '16 at 08:07
  • I understood now. Probably you better ask new question, with title something like "how to make new UIImage from two UIImage objects" – Yury Aug 09 '16 at 08:12
  • @ShadowOf actually I have such question here http://stackoverflow.com/questions/38834231/saving-two-imageviews-into-one-and-share-it-to-instagram – Anuar Maratkhan Aug 09 '16 at 08:23
  • It looks pretty unclear or hard to read. You may try to make your question very clear, without unneeded context – Yury Aug 09 '16 at 08:46

0 Answers0