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

时间:2015-12-09 06:08:59

标签: ios swift email uicollectionview

我从照片库中选择了一些照片并填充到了collectionView中。然后我的收藏视图里面会有一些照片。 这是我将照片放入集合视图的代码。

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)

1 个答案:

答案 0 :(得分:0)

从您获得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)

    }