问题描述
我正在创建自己的iMessage自定义应用程序,我只想发送一个iMessage,其背景在2张图像之间交换,因此产生了动画的错觉.我什至不确定这是否可行,但是我正在尝试下面的代码.此代码仅在收件人收到邮件时显示第一张图片.任何帮助将不胜感激.
I'm creating my own iMessage Custom App and I simply want to send an iMessage with a background that swaps between 2 images, therefore creating the illusion of animation. I'm not even sure this is possible but I'm trying with the code below. This code only shows the first image when the message is received by the recipient. Any help would be appreciated.
func createImageForMessage() -> UIImage? {
let cupAnimation = UIImageView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let imagesListArray = [UIImage(named: "boy_cup_1_1.png")!,UIImage(named: "boy_cup_1_7.png")!]
cupAnimation.animationImages = imagesListArray
cupAnimation.animationDuration = 10.0
cupAnimation.animationRepeatCount = 50
cupAnimation.startAnimating()
let cupBackground = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
cupBackground.addSubview(cupAnimation)
background.addSubview(cupBackground)
background.frame.origin = CGPoint(x: view.frame.size.width, y: view.frame.size.height)
view.addSubview(background)
UIGraphicsBeginImageContextWithOptions(background.frame.size, false, UIScreen.main.scale)
background.drawHierarchy(in: background.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
background.removeFromSuperview()
return image
}
推荐答案
iMessage的丰富消息传递扩展有两种:iMessage Apps和Sticker Pack.
Rich messaging extensions for iMessage come in two kinds: iMessage Apps and Sticker Packs.
-
拥有iMessage应用程序时,您在应用程序中创建的自定义消息内容(并通过
MSMessage
或MSSession
)可以是只有收件人也安装了您的iMessage应用程序时,收件人才能查看.如果没有,他们会收到您内容的静态预览,以及一些使接收者可以从App Store安装您的应用程序的UI.
When you have an iMessage App, the custom message content you create in your app (and send via
MSMessage
orMSSession
) can be viewed by a recipient only if they also have your iMessage app installed. If they don't, they receive a static preview of your content, along with some UI that lets the recipient install your app from the App Store.
拥有贴纸包时,创建贴纸图像不需要任何代码-它们只是资产.因此,Apple的iMessage服务可以将这些资产传递给任何设备上的任何人,任何人都可以查看它们,无论他们是否在自己的设备上安装了您的应用程序. (并且不管他们是否甚至可以安装您的应用.自定义贴纸应用只能在iOS上安装,但macOS和watchOS可以接收贴纸.)
When you have a Sticker Pack, there's no code involved in creating the sticker images — they're just assets. So Apple's iMessage service can pass those assets to anybody on any device, and anybody can view them regardless of whether they have your app installed on their device. (And regardless of whether they even could have your app installed. Custom sticker apps can be installed only on iOS, but macOS and watchOS can receive stickers.)
因此,要获得动画贴纸,您只需创建动画资产.首选的格式是 APNG ,并且Xcode支持从中自动创建APNG动画贴纸PNG图片序列-只需在编辑器中查找xcstickers
捆绑包即可.或按照Apple Developer网站上为iMessage创建贴纸上的教程和视频进行操作.
So, to have animated stickers, you just need to create animated assets. The preferred format for this is APNG, and there's support in Xcode for automatically creating APNG animated stickers from sequences of PNG images — just look in the editor for your xcstickers
bundle. Or follow along with the tutorials and videos on Creating Stickers for iMessage on the Apple Developer site.
请注意,您也可以将两者结合在一起-您可以拥有一个iMessage应用程序,该应用程序可以使用 MSSticker
类,并将其发送给其他用户.在这种情况下,贴图是动态的,因为您的应用程序是在运行时创建它们的,但是创建后它们是静态资产,这意味着iMessage服务可以分发它们,并且收件人不需要安装您的应用程序即可查看它们.
Note that you can also combine the two — you can have an iMessage app that dynamically creates new stickers with the MSSticker
class and sends them to other users. In that case, the stickers are dynamic in that your app is creating them at runtime, but after creation they're static assets, which means that the iMessage service can distribute them and recipients don't need your app installed to view them.
如果您动态创建贴纸,则需要先在应用中编写贴纸资产文件,然后再将其传递到 MSSticker
类.如果要对动画图像进行处理,则可以使用CGImageDestination
API创建APNG文件-请参阅 Apple的《图像I/O编程指南》中的这一点.
If you create stickers dynamically you'll need to write the sticker asset file in your app before passing it to the MSSticker
class. If you're doing that with animated images, you can create an APNG file using the CGImageDestination
API — see this bit in Apple's Image I/O Programming Guide.
以上内容是为了回答似乎是您问题的实际话题—在iMessage中发送动画贴纸.但是您的代码中也有一些更具体的混乱,因此,这是尝试解决此问题的方法.
The above is an attempt to answer what seems to be the actual topic of your question — sending animated stickers in iMessage. But you've also got some more specific confusion in your code, so here's an attempt to address that...
您的方法返回一个UIImage
.该类不支持动画,仅支持静态图像.如果您想对动画做任何事情,则需要一些不同的东西来表示动画(如上述CGImageDestination
所示).
Your method returns a UIImage
. That class doesn't support animations, only static images. If you want to do anything with animation, you'll need something different to represent one (as seen with CGImageDestination
above).
UIImageView
可以呈现动画.但是,当您使用drawHierarchy(in:afterScreenUpdates:)
时,您正在截取该视图的屏幕快照,并创建单个静态图像.
UIImageView
can present animations by being given a sequence of UIImage
s. But when you use drawHierarchy(in:afterScreenUpdates:)
you're taking a screenshot of that view, creating a single static image.
这篇关于动画的iMessage App图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!