在Swift中要共享到Instagram故事的图像周围的水印/边框,可以通过以下步骤实现:
以下是一个示例代码,演示了如何在Swift中添加水印/边框并共享到Instagram故事:
import UIKit
func addWatermarkToImage(image: UIImage, watermarkText: String, borderColor: UIColor) -> UIImage? {
// 创建一个图像上下文
UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0)
// 在图像上下文中绘制原始图像
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
// 绘制水印文本
let textFontAttributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 30),
NSAttributedString.Key.foregroundColor: UIColor.white,
]
let textSize = watermarkText.size(withAttributes: textFontAttributes)
let textRect = CGRect(x: image.size.width - textSize.width - 10, y: image.size.height - textSize.height - 10, width: textSize.width, height: textSize.height)
watermarkText.draw(in: textRect, withAttributes: textFontAttributes)
// 绘制边框
let borderWidth: CGFloat = 10
let borderColor = borderColor.cgColor
let borderRect = CGRect(x: borderWidth / 2, y: borderWidth / 2, width: image.size.width - borderWidth, height: image.size.height - borderWidth)
let borderPath = UIBezierPath(rect: borderRect)
borderPath.lineWidth = borderWidth
borderColor.setStroke()
borderPath.stroke()
// 从图像上下文中获取最终图像
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
// 结束图像上下文
UIGraphicsEndImageContext()
return finalImage
}
// 加载要共享的图像
let originalImage = UIImage(named: "image.jpg")!
// 添加水印/边框
let watermarkedImage = addWatermarkToImage(image: originalImage, watermarkText: "Watermark", borderColor: UIColor.red)
// 共享到Instagram故事
if let imageToShare = watermarkedImage {
let activityViewController = UIActivityViewController(activityItems: [imageToShare], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.assignToContact]
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = self.view.bounds
}
self.present(activityViewController, animated: true, completion: nil)
}
这是一个简单的示例代码,演示了如何在Swift中添加水印/边框并共享到Instagram故事。你可以根据自己的需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云