Swift:使用UIActivityViewController共享图像

时间:2016-11-09 09:53:20

标签: ios swift xcode8 uiactivityviewcontroller mfmailcomposeviewcontroller

我想使用ActivityViewController在按钮上分享QR图像。

Screenshot 1

以下是我用过的代码:

@IBAction func btnShareQRCode_Clicked(sender: UIButton) {
        self.shareQRCodeUsingActivityViewController(self.imageviewQRCode.image!)
    }

func shareQRCodeUsingActivityViewController(imageParamater: UIImage) {
        let activityItem: [UIImage] = [imageParamater as UIImage]

        let objActivityViewController = UIActivityViewController(activityItems: activityItem as [UIImage], applicationActivities: nil)
        objActivityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]

        // objActivityViewController.popoverPresentationController?.sourceView = sender
        self.presentViewController(objActivityViewController, animated: true, completion: {
            objActivityViewController.completionWithItemsHandler = { activity, success, items, error in

                if !success { print("cancelled")
                    return
                }

                if activity == UIActivityTypeMail {
                    print("mail")
                }
                else if activity == UIActivityTypeMessage {
                    print("message")
                }
                else if activity == UIActivityTypeSaveToCameraRoll {
                    print("camera")
                }
            }
        })
    }

    func completionHandler() {
    }

这个问题是它在邮件上崩溃,说明有关MailComposer的错误。

我想知道应该如何以及在何处处理这些MailComposer函数?

1 个答案:

答案 0 :(得分:1)

如果您在iOS模拟器上运行此功能,则邮件组件可能会失败。

除此之外,我认为您不需要将活动项目列表转换为UIImage。只需将一个对象数组作为activityItems数组。