Swift - 从图像创建GIF并将其转换为NSData [英] Swift - Create a GIF from Images and turn it into NSData

查看:459
本文介绍了Swift - 从图像创建GIF并将其转换为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个业余问题,但是虽然我已经可扩展地搜索了Stack Overflow,但我无法得到我特定问题的答案。

This might be an amateur question, but although I have searched Stack Overflow extensibly, I haven't been able to get an answer for my specific problem.

I通过遵循Github示例成功地从图像数组创建GIF文件:

I was successful in creating a GIF file from an array of images by following a Github example:

func createGIF(with images: [NSImage], name: NSURL, loopCount: Int = 0, frameDelay: Double) {

    let destinationURL = name
    let destinationGIF = CGImageDestinationCreateWithURL(destinationURL, kUTTypeGIF, images.count, nil)!

    // This dictionary controls the delay between frames
    // If you don't specify this, CGImage will apply a default delay
    let properties = [
        (kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): frameDelay]
    ]


    for img in images {
        // Convert an NSImage to CGImage, fitting within the specified rect
        let cgImage = img.CGImageForProposedRect(nil, context: nil, hints: nil)!

        // Add the frame to the GIF image
        CGImageDestinationAddImage(destinationGIF, cgImage, properties)
    }

    // Write the GIF file to disk
    CGImageDestinationFinalize(destinationGIF)
}

现在,我想转向实际的GIF到NSData,所以我可以将它上传到Firebase,并能够在另一台设备上检索。

Now, I would like to turn the actual GIF into NSData so I can upload it to Firebase, and be able to retrieve it on another device.

为了实现我的目标,我有两个选择:要么找到如何使用上面的代码来提取创建的GIF(这似乎是在创建文件时直接创建),或者使用函数参数上的图像来创建新的GIF,但保持NSData格式。

To achieve my goal, I have two options: Either to find how to use the code above to extract the GIF created (which seems to directly be created when creating the file), or to use the images on the function's parameters to create a new GIF but keep it on NSData format.

有没有人对如何做到这一点有任何想法?

Does anybody have any ideas on how to do this?

推荐答案

由于没有人继续超过六个月我将在这里给出@Sachin Vas评论的答案:

Since nobody went ahead for over six months I will just put the answer from @Sachin Vas' comment here:

你可以使用 NSData获取数据(contentsOf:URL )

这篇关于Swift - 从图像创建GIF并将其转换为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆