Sharing images with UIActivityViewController doesn't work

I've created this function for sharing image, but I have a problem.

func shareImg(img: UIImage) {

        let imageShare = [ img ]

        let activityViewController = UIActivityViewController(activityItems: imageShare as [Any] , applicationActivities: nil)

        activityViewController.popoverPresentationController?.sourceView = self.view

        activityViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

        activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)

        activityViewController.completionWithItemsHandler = { (type, _, _, error) in

            print(error)

        }

        self.present(activityViewController, animated: true, completion: nil)

    }

Every time I try to save image to photo library it gives me this error and it isn't saved.:

Error Domain=ALAssetsLibraryErrorDomain Code=-1 "Unknown error" UserInfo={NSLocalizedDescription=Unknown error, NSUnderlyingError=0x282e0ff60 {Error Domain=PHPhotosErrorDomain Code=3303 "(null)"}}

I've found that code 3303 refers to:

ALAssetsLibraryWriteIncompatibleDataError

And also it logs this into console:

[ShareSheet] connection invalidated

Can anyone please help me? Thanks in advance

Answered by psrajer in 690940022

I've solved it. The problem was that the image was ciimage. The problem is described here: https://www.hackingwithswift.com/forums/swift/silent-failure-of-uiimagewritetosavedphotosalbum/872

In your case, as Any is not needed as you have a single type. But that's not the cause of error.

If you pass an UIImage like this, it crashes, because writing to the photo library is a restricted operation.

Find how to implement here: h t t p s : / / w w w.hackingwithswift.com/articles/118/uiactivityviewcontroller-by-example

Accepted Answer

I've solved it. The problem was that the image was ciimage. The problem is described here: https://www.hackingwithswift.com/forums/swift/silent-failure-of-uiimagewritetosavedphotosalbum/872

Sharing images with UIActivityViewController doesn't work
 
 
Q