将CollectionViewView照片传递给电子邮件附件iOS swift

我从照片库中select了一些照片,并填充到collectionView中。 那么我的collections视图里面会有一些照片。 这里是我的代码获取照片到collections视图。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell: PhotoCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath) as! PhotoCollectionViewCell let asset : PHAsset = self.photoAsset[indexPath.item] as! PHAsset PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in if let image = result { cell.setThumbnailImage(image) } }) return cell } 

但是,如何将位于集合视图中的所有这些照片传递给电子邮件附件? 下面的代码是电子邮件附件,我如何将所有的照片传递给这个附件?

 let emailTitle = "Email Us" let messageBody = "Location: \(sendLocation) \n\n \(sendContent)" let toReceipients = ["testing@gmail.com"] let mc : MFMailComposeViewController = MFMailComposeViewController() mc.mailComposeDelegate = self mc.setSubject(emailTitle) mc.setMessageBody(messageBody, isHTML: false) mc.setToRecipients(toReceipients) self.presentViewController(mc, animated: true, completion: nil) 

您可以通过获取图像的相同方式获取PHImageManager ,您可以获取所有这些图像,并附加到邮件中。 或者如果你想附加所有的图像,那么你可以将所有加载的图像添加到一个数组,并附加这些邮件,如:

 let asset : PHAsset = self.photoAsset[indexPath.item] as! PHAsset PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFill, options: nil, resultHandler: {(result, info)in if let image = result { cell.setThumbnailImage(image) self.arr.append(image) } }) 

然后将所有的图像从arr添加到邮件编辑器中

 func composeMail() { let mailComposeVC = MFMailComposeViewController() for image in arr { let photoData : NSData = UIImagePNGRepresentation(image)! mailComposeVC.addAttachmentData(UIImageJPEGRepresentation(photoData, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "test.jpeg") } mailComposeVC.setSubject("Email Subject") self.presentViewController(mailComposeVC, animated: true, completion: nil) }