我有以下代码,允许UIButton捕获部分屏幕截图并将其保存到相机胶卷 - 谢谢Lou Franco:)
// Declare the snapshot boundaries
let top: CGFloat = 100
let bottom: CGFloat = 60
// The size of the cropped image
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)
// Start the context
UIGraphicsBeginImageContext(size)
// we are going to use context in a couple of places
let context = UIGraphicsGetCurrentContext()!
// Transform the context so that anything drawn into it is displaced "top" pixels up
// Something drawn at coordinate (0, 0) will now be drawn at (0, -top)
// This will result in the "top" pixels being cut off
// The bottom pixels are cut off because the size of the of the context
CGContextTranslateCTM(context, 0, -top)
// Draw the view into the context (this is the snapshot)
view.layer.renderInContext(context)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
// End the context (this is required to not leak resources)
UIGraphicsEndImageContext()
// Save to photos
UIImageWriteToSavedPhotosAlbum(snapshot, nil, nil, nil)
我如何修改上述代码,以便:
我意识到我的问题可能让我看起来很懒惰,不是这样,我现在只是非常环保:)
我已经尝试过搜索网络并修改各种代码段,但我真的很难过!
非常感谢提前。非常感谢您的时间和精力!
答案 0 :(得分:1)
以下代码将为您设置电子邮件图像。
let composer = MFMailComposeViewController()
composer.setToRecipients(["someemail@email.com"])
composer.setMessageBody("Body", isHTML: false)
composer.setSubject("Subject")
let imageData = UIImagePNGRepresentation(snapshot)
composer.addAttachmentData(imageData, mimeType: "image/png", fileName:"myImage")
presentViewController(composer, animated: true, completion: nil)
要将其作为短信发送,它几乎完全相同,除非您使用MFMessageComposeViewController
而不是MFMailComposeViewController