我试图通过电子邮件发送一个PHAsset图像。我首先尝试在一个沙箱应用程序中获得一个基本案例,然后我想最终迭代一个PHAssets数组。下面是我的沙箱代码,然后是几个问题。(其中一些代码片段来自这是如此的帖子。)这都是为了实现我发现的名为TLPHAssets的多资产选择器,它看起来是这张名单在另一篇文章上最新版本的Swift-4 / PHAsset。
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")
}
})有两个问题(我对斯威夫特来说是个新手,所以如果我错过了一些基本的问题,很抱歉):
任何和所有的帮助都非常感谢。
发布于 2017-11-06 17:24:27
我想通了。关键是将isSynchronous设置为PHImageRequestOptions中的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")
}
})
}https://stackoverflow.com/questions/46965052
复制相似问题