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

Accepted Reply

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

Replies

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

  • @Claude31 Thanks for your response. I've updated the code as you mentioned and looked on the url you sent me. I've found out that If I try to save some image with systemName it saves it to gallery with no error. So I suppose there is a problem with that photo (It is a QR code image). It is weird that I can send it as a message but cannot save it.

Add a Comment

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