我在将base 64字符串转换为gif时遇到问题,我尝试了以下内容。unbased, err := base64.StdEncoding.DecodeString(bs64) if err != nil { panic("Cannot decode b64") } var imgCoupon image.Image imgCoupon, err = gif.Decode(bytes.NewReader(unbased)) var opt gif.Options opt.NumColors = 256 var buff bytes.Buffer gif.Encode(&buff, imgCoupon, &opt)但是当我将其上传到GCP / 谷歌云存储中时,GIF不会动画。这就是我上传的方式。sw := storageClient.Bucket(bucket).Object("test"+"_file"+".gif").NewWriter(ctx)if _, err := io.Copy(sw, &buff); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": err.Error(), "error": true, }) return}结果:它应该如何:
1 回答

慕的地10843
TA贡献1785条经验 获得超8个赞
问题在于它是如何被解码的。 返回一个 ,但 gif。解码全部返回一个类型。gif.Decodeimage.Imagegif.GIF
同样,用于写入多个帧。gif.EncodeAll
这是适合我的代码:
unbased, err := base64.StdEncoding.DecodeString(bs64)
if err != nil {
panic("Cannot decode b64")
}
// Use DecodeAll to load the data into a gif.GIF
imgCoupon, err := gif.DecodeAll(bytes.NewReader(unbased))
imgCoupon.LoopCount = 0
// EncodeAll to the buffer
var buff bytes.Buffer
gif.EncodeAll(&buff, imgCoupon)
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消