Swift 3通过电子邮件发送PHAsset图像

时间:2017-10-26 22:55:49

标签: ios swift3 phasset

我正在尝试通过电子邮件发送PHAsset图像。我首先尝试在沙盒应用程序中获取基本案例,然后我想最终迭代一组PHAssets。下面是我的沙箱代码,接下来是我遇到的几个问题/问题。 (此代码段中的一些来自this SO post。)这是尝试实现我发现的名为TLPHAssets的多资产选择器的所有部分,它看起来是最快的Swift-4 / PHAsset当前来自this list on another SO post的版本。

func dismissPhotoPicker(withTLPHAssets: [TLPHAsset]) {
    // use selected order, fullresolution image

    self.selectedAssets = withTLPHAssets

    let mail = MFMailComposeViewController()
    mail.mailComposeDelegate = self;
    mail.setToRecipients(["emailaddress.com"])
    // Put the rest of the email together and present the draft to the user
    mail.setSubject("Subject")

    let options = PHImageRequestOptions()
    options.isNetworkAccessAllowed = true
    options.version = .current
    options.deliveryMode = .opportunistic
    options.resizeMode = .fast
    let asset : PHAsset = self.selectedAssets[0].phAsset! as PHAsset
    PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width : 400, height : 400), contentMode: .aspectFit, options: options, resultHandler: {(result: UIImage!, info) in
        if let image = result
        {
            let imageData: NSData = UIImageJPEGRepresentation(image, 1.0)! as NSData
            mail.addAttachmentData(imageData as Data, mimeType: "image/jpg", fileName: "BeforePhoto.jpg")
        }
    })

两个问题(我对Swift很新,如果我错过了一些基本的东西,请道歉):

  1. 我无法让resultHandler代码块被执行。我将targetSize设置为PHImageManagerMaximumSize,但是在另一篇文章的推荐下将其更改为400x400(顺便说一下,我只是从我的相机胶卷中挑选一张图片并且我已经在我的测试用例中确认了PHAsset的mediaType是' image&#39 ;;还没有使用Live Photo或Video,虽然我打算解决下一个问题,但我会添加一个switch语句来确保我调用正确的资产-request Handler)。为什么PHImageManagerMaximumSize不起作用?当我在模拟器中运行应用程序并选择五个预制图像中的一个时,我仍然无法进入resultHandler代码块(这些都是mediaType' image')。知道我的requestImage调用可能出现什么问题,导致无法调用resultHandler?

  2. 我最终从我的相机胶卷中获取了一张图像,以便使用400x400的targetSize调用resultHandler代码。但是现在在resultHandler中,我的沙箱应用程序崩溃在"让imageData:NSData ..."与" 2017-10-26 15:35:26.604997-0700一起使用MultiAssetPicker [15766:4904592]致命错误:在展开可选值"时意外发现nil。

  3. 非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。关键是要在PHImageRequestOptions中将isSynchronous设置为true。下面介绍了解决这些问题的最新代码,尽管我还没有将其称为生产(需要更多测试)。

                    // Set up the image-request options
                    let options = PHImageRequestOptions()
                    options.isNetworkAccessAllowed = true
                    options.version = .current
                    options.deliveryMode = .opportunistic
                    options.isSynchronous = true
                    options.resizeMode = .fast

                    // For each photo asset the user selected
                    for i in 0..<self.selectedAssets.count {

                        // Convert to a PHAsset
                        let asset : PHAsset = self.selectedAssets[i].phAsset! as PHAsset

                        // Request the underlying asset as an image
                        PHImageManager.default().requestImage(for: asset, targetSize: CGSize(width : 400, height : 400), contentMode: .aspectFill, options: options, resultHandler: {(result: UIImage!, info) in

                            // If the asset is in fact an image, attach it to the email being composed
                            switch self.selectedAssets[i].phAsset?.mediaType {
                            case .image?:
                                print("Image \(i)")
                                let imageData: NSData = UIImageJPEGRepresentation(result!, 0.9)! as NSData
                                mail.addAttachmentData(imageData as Data, mimeType: "image/jpg", fileName: "Attachment\(i).jpg")
                            case .video?:
                                print("Video not supported.")
                            case .audio?:
                                print("Audio not supported")
                            default:
                                print("Unknown")
                            }
                        })
                    }