0
votes

I'm basically taking a screenshot of a view, saving it to my camera roll , getting the NSURL path and exporting to Whatsapp

    //Create the UIImage
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Save it to camera roll round 2
    var library = ALAssetsLibrary()

    library.writeImageToSavedPhotosAlbum(image.CGImage, metadata: nil, completionBlock: {
        (path:NSURL!, error:NSError!) -> Void in

        if NSThread.currentThread() == NSThread.mainThread(){
            println("\(path)")
            var controller = UIDocumentInteractionController()
            controller.delegate = self
            controller.UTI = "net.whatsapp.image"
            controller.URL = path
            controller.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

        }
    })

I'm getting this error :

Assertion failure in -[UIDocumentInteractionController setURL:], /SourceCache/UIKit/UIKit-3318.16.21/UIDocumentInteractionController.m:1024

But according to Apple's Documentation , it has to be a NSURL (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/#//apple_ref/occ/instp/UIDocumentInteractionController/URL)

Any ideas?

1
But it cannot be nil. Is it?matt
BTW, do not write if NSThread.currentThread() == NSThread.mainThread(). Use isMainThread. Even better, don't even ask that question; if your goal is to get onto the main thread, step out onto the main thread (with dispatch_async).matt
@matt thanks for the feedback! It was just a quick check I made to see if the completionBlock was running on the mainThread. I'll be using isMainThread from now on.Tialtous
But what if it isn't the main thread? You still want to do this. So do what I said; step out with dispatch_async.matt

1 Answers

2
votes

Once a image is saved in camera roll or somewhere outside of your application, you can access it using URL with 'assets-library scheme'.

But UIDocumentInteractionController's URL property only supports URL with 'file scheme'.

So you need to save your image to your application's temporary directory and set 'path' property to it's file scheme URL.