我有以下功能,可以在将图片分享到instagram故事时正常工作
private func shareToInstagramStories() {
guard let imagePNGData = UIImage(data: imageData!)?.pngData() else { return }
guard let instagramStoryUrl = URL(string: "instagram-stories://share") else { return }
guard UIApplication.shared.canOpenURL(instagramStoryUrl) else { return }
let itemsToShare: [[String: Any]] = [["com.instagram.sharedSticker.backgroundImage": imagePNGData]]
let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]
UIPasteboard.general.setItems(itemsToShare, options: pasteboardOptions)
UIApplication.shared.open(instagramStoryUrl, options: [:], completionHandler: nil)
}我想尝试和做的是能够在带有品牌的图像上放置一个边框/水印。可以在上面的函数中添加这个吗?还是先调用"watermark“函数,然后将结果传递给shareToInstagramStories函数?
谢谢
发布于 2020-05-26 21:04:33
找出对我有效的方法:
func generateImageWithText() -> UIImage? {
let image = UIImage(data: imageData!)!
let imageView = UIImageView(image: image)
imageView.backgroundColor = UIColor.clear
imageView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let btnDetail = UIButton(frame: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
btnDetail.setTitle("watermarkText", for: .normal)
btnDetail.titleLabel?.font = .systemFont(ofSize: 72)
btnDetail.titleLabel?.numberOfLines = 5
btnDetail.titleLabel?.lineBreakMode = .byCharWrapping
btnDetail.contentVerticalAlignment = .top
btnDetail.contentHorizontalAlignment = .left
btnDetail.isUserInteractionEnabled = false
btnDetail.autoresizesSubviews = true
btnDetail.autoresizingMask = .flexibleWidth
UIGraphicsBeginImageContextWithOptions(btnDetail.bounds.size, false, 0)
imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
btnDetail.layer.render(in: UIGraphicsGetCurrentContext()!)
let imageWithText = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageWithText
}
func shareToInstagramStories() {
guard let imagePNGData = generateImageWithText()!.pngData() else { return }
guard let instagramStoryUrl = URL(string: "instagram-stories://share") else { return }
guard UIApplication.shared.canOpenURL(instagramStoryUrl) else { return }
let itemsToShare: [[String: Any]] = [["com.instagram.sharedSticker.stickerImage": imagePNGData]]
let pasteboardOptions: [UIPasteboard.OptionsKey: Any] = [.expirationDate: Date().addingTimeInterval(60 * 5)]
UIPasteboard.general.setItems(itemsToShare, options: pasteboardOptions)
UIApplication.shared.open(instagramStoryUrl, options: [:], completionHandler: nil)
}我使用isUserInteractionEnabled = false的UIButton,而不是UILabel,因为我希望文本具有一些垂直对齐方式。
希望这能帮助到其他人
https://stackoverflow.com/questions/62017351
复制相似问题